I want to spin-up a docker for mongodb:latest
but allow only certain user(s) to access certain db(s) (i.e. enable --auth
). No one else should access mongodb whatsoever! How should I do this as part of the docker initiation?
BTW, data directory
sits on the host by utilising the following command during initiation: -v /my/own/datadir:/data/db
.
Here's what I did for the same problem, and it worked.
Run the mongo docker instance on your server
Open bash on the running docker instance.
Reference- https://github.com/arunoda/meteor-up-legacy/wiki/Accessing-the-running-Mongodb-docker-container-from-command-line-on-EC2
Enter the mongo shell by typing mongo.
For this example, I will set up a user named ian and give that user read & write access to the cool_db database.
Reference: https://ianlondon.github.io/blog/mongodb-auth/ (First point only)
Exit from mongod shell and bash.
Stop the docker instance using the below command.
Now run the mongo docker with auth enabled.
Reference: How to enable authentication on MongoDB through Docker? (Usman Ismail's answer to this question)
I was able to connect to the instance running on a Google Cloud server from my local windows laptop using the below command.
Reference: how can I connect to a remote mongo server from Mac OS terminal
Just dropping a .js file into the entry point init folder works for this
e.g. entrypoint.js
docker-compose.yml:
Doing the rest by hand or more of the same works.
If you want to you can also drop a .sh file into the init folder to clean up the files so they are not laying around: zz-cleanup.sh.
a. You can use environment variables via terminal:
b. You can use environment variables in your docker stack deploy file or compose file for versions 3.4 through 4.1.
As it is explained on the quick reference section of the official mongo image set
MONGO_INITDB_ROOT_USERNAME
andMONGO_INITDB_ROOT_PASSWORD
in your yaml file:docker-entrypoint.sh file in mongo image checks for the existence of these two variables and sets
--auth
flag accordingly.c. You can also use docker secrets.
MONGO_INITDB_ROOT_USERNAME
andMONGO_INITDB_ROOT_PASSWORD
is set indirectly by docker-entrypoint.sh fromMONGO_INITDB_ROOT_USERNAME_FILE
andMONGO_INITDB_ROOT_PASSWORD_FILE
variables:docker-entrypoint.sh converts
MONGO_INITDB_ROOT_USERNAME_FILE
andMONGO_INITDB_ROOT_PASSWORD_FILE
toMONGO_INITDB_ROOT_USERNAME
andMONGO_INITDB_ROOT_PASSWORD
.You can use
MONGO_INITDB_ROOT_USERNAME
andMONGO_INITDB_ROOT_PASSWORD
in your.sh
or.js
scripts indocker-entrypoint-initdb.d
folder while initializing database 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 theMONGO_INITDB_DATABASE
variable, if it is present, or test otherwise. You may also switch databases within the.js
script.This last method is not in the reference docs, so it may not survive an update.
The Dockerfile for the official mongo image is here. The default command is mongod but you can override to add the --auth switch assuming user's are already configured.
If the user has to be created then you need to volume mount a startup script into
/entrypoint.sh
to replace the default startup script and then have that script create users and start mongo with the auth switch.use this images to fix:
With docker-compose.yml
If you take a look at:
you will notice that there are two variables used in the
docker-entrypoint.sh
:You can use them to setup root user. For example you can use following
docker-compose.yml
file:Now when starting the container by
docker-compose up
you should notice following entries:To add custom users apart of root use the entrypoint exectuable script (placed under $PWD/mongo-entrypoint dir as it is mounted in
docker-compose
to entrypoint):Entrypoint script will be executed and additional users will be created.