Docker container doesnt initialize default databases when using environment variables

Hi, so I’m relatively new to mongoDB and docker and I’m trying to spin up a mongoDB container to use for testing/development. However like the title says I’ve noticed that when passing in environment variables MONGO_INITDB_ROOT_USERNAME , MONGO_INITDB_ROOT_PASSWORD to automatically set admin name and password the container doesn’t create the default databases admin, config, local. Im assuming this is an issue as (again making an assumption here) admin database is supposed to store the administrator/root user and their password no? Should I just create these databases manually?

Also I can’t use version 5 because of CPU issues. I saw an issue on github where certain older cpu’s don’t/wont allow the container to initialize and this seems to be a issue with me as well as I’m running this on an older machine and I spent way too much time trying to get 5 running before giving up and using 4.4 with no issues what so ever.

Could not reproduce. Had you run this container before without the environment variables with the same /data/db files?

The docker-entrypoint.sh script will not do any initialization if is discovers some telltale files.


docker run --rm -it \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=solarwinds1234 \
-v mdb-data:/data/db \
-p 127.0.0.1:27017:27017 \
mongo:4.4
mongosh --quiet
test> show dbs
MongoServerError: command listDatabases requires authentication
test> use admin
switched to db admin
admin> db.auth('root','solarwinds1234')
{ ok: 1 }
admin> show dbs
admin    164 kB
config  61.4 kB
local   73.7 kB

Ok so from my understanding the --rm removed the container as soon as it’s exited correct? When I try to run your first paragraph i just get a bunch of log output and then I cant input anything I have to cntrl+C to get out and it just shuts down on me.

can you please try :

docker run -d --name test \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=solarwinds1234 \
-v mdb-data:/data/db \
-p 127.0.0.1:27017:27017 \
mongo:4.4

then

docker exec -it test bash
mongo 
show dbs

and let me know if it initializes anything?

I’m doing it step by step but eventually I also want to add -v /mnt/externalHD/mongoDB:/data/dd so that i can access teh db from outside as well.

And no I tried MANY times if i used the same volume I made sure to delete it first and if mounting same directory on host system I made sure to delete all files inside.

Also I wanted to edit my previous post to add this info in but couldn’t find the edit button… it’s not possible to edit posts here?

youll notice that if you run

docker run -d --name test \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=solarwinds1234 \
-v mdb-data:/data/db \
-p 127.0.0.1:27017:27017 \
mongo:4.4

no databases are created
However if you run

docker run -d --name test \
-v mdb-data:/data/db \
-p 127.0.0.1:27017:27017 \
mongo:4.4

admin,local,config databases are created.

Unless I am missing something or doing something completely wrong in which case can anyone please tell me what I am doing wrong?

Nevermind I figured out what I was doing wrong.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.