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
AppDeveloper
  • 1,816
  • 7
  • 24
  • 49

1 Answers1

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

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • oh so you have to write code like `db.article.save` for each record? so no json solution? – AppDeveloper Jul 26 '19 at 07:21
  • also any comments on the part 2 of my problem – AppDeveloper Jul 26 '19 at 07:23
  • seem from documentation, you can override entry point to deal with a json file. – Adiii Jul 26 '19 at 07:35
  • 1
    the database will only be created if there is any file `/dockerentrypoint.d` otherwise no database will be created. see documentation – Adiii Jul 26 '19 at 07:37
  • `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? – AppDeveloper Jul 26 '19 at 07:40
  • you can try without it, i provide it just an example to make your point clear – Adiii Jul 26 '19 at 07:47
  • Thanks for the answer, quick question btw, for instance in your example `comments` field lists `author` name, how about if I want to add another field `_id` of the author and include with `comments`. Is that how's its usually done in document databases, if then how would you do that. I'm new to JSON docs and coming from the relational background. – AppDeveloper Jul 27 '19 at 03:18
  • are you asking about something like `comments : [ { author :"will" , text : "i don't like the color" ,author_id:1}` – Adiii Jul 27 '19 at 03:27
  • yes or put simply use the generated value from an earlier insert, it'd look like `comments : [ { _id: "5d3bc38b18f5f0b3ca543822", author :"will" , text : "i don't like the color"}]`, like primary key and foreign key from traditional/relational db systems. – AppDeveloper Jul 27 '19 at 03:30
  • I found a video, https://www.youtube.com/watch?v=7cYT0Qbu9tU please let me know if if I should know something else too – AppDeveloper Jul 27 '19 at 03:39