I am working with Docker and I have a stack with PHP, MySQL, Apache and Redis. I need to add MongoDB now so I was checking the Dockerfile for the latest version and also the docker-entrypoint.sh file from the MongoDB Dockerhub but I couldn't find a way to setup a default DB, admin user/password and possibly auth method for the container from a docker-compose.yml
file.
In MySQL you can setup some ENV variables as for example:
db:
image: mysql:5.7
env_file: .env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
And this will setup the DB and the user/password as the root
password.
Is there any way to achieve the same with MongoDB? Anyone has some experience or workaround?
Here another cleaner solution by using
docker-compose
and ajs
script.This example assumes that both files (docker-compose.yml and mongo-init.js) lay in the same folder.
docker-compose.yml
mongo-init.js
Then simply start the service by running the following docker-compose command
The official
mongo
image has merged a PR to include the functionality to create users and databases at startup.The database initialisation will run when there is nothing populated in the
/data/db
directory.Admin User Setup
The environment variables to control "root" user setup are
MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD
Example
You don't need to/can't use
--auth
on the command line as the docker entrypoint.sh script adds this in when the environment variables exist.Other Custom Initialisation
The image also provides the
/docker-entrypoint-initdb.d/
path to deploy custom.js
or.sh
setup scripts that will be run once on database initialisation..js
scripts will be run againsttest
by default orMONGO_INITDB_DATABASE
if defined in the environment.or
A simple initialisation javascript file that demonstrates logging and how to exit with an error (for result checking).
In case someone is looking for how to configure MongoDB with authentication using
docker-compose
, here is a sample configuration using environment variables:When running
docker-compose up
your mongo instance is run automatically with auth enabled. You will have a admin database with the given password.Mongo image can be affected by
MONGO_INITDB_DATABASE
variable, but it won't create the database. This variable determines current database when running/docker-entrypoint-initdb.d/*
scripts. Since you can't use environment variables in scripts executed by Mongo, I went with a shell script:docker-cloud.yml
:init-mongo.sh
:Here's a working solution that creates
admin-user
user with a password, additional database (test-db
), andtest-user
in that database.Dockerfile:
mongo-init.js:
The tricky part was to understand that *.js files were run unauthenticated. The solution authenticates the script as the
admin-user
in theadmin
database.MONGO_INITDB_DATABASE admin
is essential, otherwise the script would be executed against thetest
db. Check the source code of docker-entrypoint.sh.If you are looking to remove usernames and passwords from your docker-compose.yml you can use Docker Secrets, here is how I have approached it.
I have use the file: option for my secrets however, you can also use external: and use the secrets in a swarm.
The secrets are available to any script in the container at /var/run/secrets
The Docker documentation has this to say about storing sensitive data...
https://docs.docker.com/engine/swarm/secrets/
You can use secrets to manage any sensitive data which a container needs at runtime but you don’t want to store in the image or in source control, such as:
Usernames and passwords TLS certificates and keys SSH keys Other important data such as the name of a database or internal server Generic strings or binary content (up to 500 kb in size)