Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use MongoDB with docker-compose: create database and user

Tags:

docker

pymongo

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)

1 Answers

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

like image 70
Adiii Avatar answered Dec 09 '25 11:12

Adiii