4

I've the following script with a custom database specified but I don't see the database user getting created within the GUI (compass). I only see 3 default databases (admin, config, local).

I've looked into this linked answer but I need a specific answer for my question, please.

mongo:
    image: mongo:4.0.10
    container_name: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: mypass
      MONGO_INITDB_DATABASE: mydb
    ports:
      - 27017:27017
      - 27018:27018
      - 27019:27019
  1. The expectation for a user database to be created.
  2. Database prefilled with some records.

Edit - made some progress, 2 Problems

Added volumes

mongo:
  image: mongo:4.0.1r0
  container_name: mongo
  restart: always
  volumes:
    - ./assets:/docker-entrypoint-initdb.d/

1. Ignore

Within assets folder, I've 3 files and I see this in the logs, my files are getting ignored.

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file1.json

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file2.json

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file3.json

all my JSON files look like following. (no root array object? no [] at the root?)

{ "_id" : { "$oid" : "5d3a9d423b881e4ca04ae8f0" }, "name" : "Human Resource" }
{ "_id" : { "$oid" : "5d3a9d483b881e4ca04ae8f1" }, "name" : "Sales" }

2. Default Database not getting created. following line is not having any effect.

MONGO_INITDB_DATABASE: mydb
3
  • Do you put your scripts in scripts in /docker-entrypoint-initdb.d/? Commented Jul 26, 2019 at 6:04
  • please see my edits Commented Jul 26, 2019 at 6:57
  • @AppDeveloper data should in .js or .sh file, rest will be ignored. you can look at the example I provided. Commented Jul 26, 2019 at 7:18

1 Answer 1

2

All files *.json extension will be ignored, it should in *.js. Look into the documentation of mongo DB docker hub

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 fundamental 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.

you can look into this example

create folder data and place create_article.js in it

( in the example I am passing your created DB user)

db = db.getSiblingDB("user");
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 }
});

db.article.save( {
    title : "this is your title" , 
    author : "dave" , 
    posted : new Date(4121381470000) , 
    pageViews : 7 , 
    tags : [ "fun" , "nasty" ] ,
    comments : [ 
        { author :"barbara" , text : "this is interesting" } , 
        { author :"jenny" , text : "i like to play pinball", votes: 10 } 
    ],
    other : { bar : 14 }
});

db.article.save( {
    title : "this is some other title" , 
    author : "jane" , 
    posted : new Date(978239834000) , 
    pageViews : 6 , 
    tags : [ "nasty" , "filthy" ] ,
    comments : [ 
        { author :"will" , text : "i don't like the color" } , 
        { author :"jenny" , text : "can i get that in green?" } 
    ],
    other : { bar : 14 }
});

mount the data directory

docker run --rm -it  --name some-mongo -v /home/data/:/docker-entrypoint-initdb.d/  -e MONGO_INITDB_DATABASE=user     -e MONGO_INITDB_ROOT_USERNAME=root     -e MONGO_INITDB_ROOT_PASSWORD=mypass  mongo:4.0.10

once container created you will be able to see the DBs,

enter image description here

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

10 Comments

oh so you have to write code like db.article.save for each record? so no json solution?
also any comments on the part 2 of my problem
seem from documentation, you can override entry point to deal with a json file.
the database will only be created if there is any file /dockerentrypoint.d otherwise no database will be created. see documentation
db = db.getSiblingDB("employeeadmin");, on a side note, db seems to be global object, but then here it gets re-assigned from whatever getSiblingDB returns?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.