3

Is possible to create a database using docker-compose? I'm trying to run mongodb on docker but I'm not able to create user and initial database :(

version: '3.1'

services:
  mongo:
    image: mongo
    restart: always
    environment:
      - MONGO_INITDB_DATABASE=test
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    volumes:    
      - ~/docker/volumes/mongodb:/data/db
    ports:
      - 27017:27017

What is missing in my script?

test code:

from pymongo import MongoClient

mongo_client = MongoClient('mongodb://%s:%[email protected]' % ('admin', 'admin'))
cursor = mongo_client.list_databases()
for db in cursor:
    print(db)

2 Answers 2

3

The above docker-compose seems fine, it will create a user named admin and the DB named will be test if your *.js file contain insert data script. otherwise, it will not create Database because so if you do not insert data with your JavaScript files, then no database is created.

You can log in with these credential that specifies in env.

  - MONGO_INITDB_DATABASE=test
  - MONGO_INITDB_ROOT_USERNAME=test
  - MONGO_INITDB_ROOT_PASSWORD=admin

To initialize DB, you need to mount you DB init script.

    volumes:    
      - ~/docker/volumes/mongodb:/data/db
      - youdbscript/init.js:/docker-entrypoint-initdb.d/

MONGO_INITDB_DATABASE

This variable allows you to specify the name of a database to be used for creation scripts in /docker-entrypoint-initdb.d/*.js (see Initializing a fresh instance below). MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.

Initializing a fresh instance

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.

mongo-docker

Update:

For your information the code you pasted code is not js file. its python script. Also, change the user name from admin might conflict with admin.

from pymongo import MongoClient

mongo_client = MongoClient('mongodb://%s:%[email protected]' % ('admin', 'admin'))
cursor = mongo_client.list_databases()
for db in cursor:
    print(db)

You init script will some thing like

youdbscript/init.js

You can try this.

db = db.getSiblingDB("test");
db.article.drop();

db.article.save( {
    title : "this is my title" , 
    author : "bob" , 
    posted : new Date(1079895594000) , 
    pageViews : 5 , 
    tags : [ "fun" , "good" , "fun" ] ,
    comments : [ 
        { author :"joe" , text : "this is cool" } , 
        { author :"sam" , text : "this is bad" } 
    ],
    other : { foo : 5 }
});

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Should I add some .js file?
0

Just like @Adiii answer. If you just declare a db name and not insert some data. The db will not created actually.

As your question. You can add one simple script (such as .sh or .js script) to the path /docker-entrypoint-initdb.d. Like the docs on mongo Initializing a fresh instance section.

9 Comments

I can't even connect using Robo 3T. User admin/admin is not being created too.
Still not working. Geez...a pain to deploy a simple damn container
Not feel depressed. can you show me the error info?
Dont have an error. When I try to connect to db admin using credentials admin/admin, I got an authorization error on Robo 3T
First. the test code you parsed run normally?Have you see the docker container use the docker ps command? Have you rebuild the container? I have use you code to do. Anything is correct ! also the robo 3t. Just try to rebuild more times.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.