Run MongoDB Authenticated via Docker Compose
If you are using the official mongo image, you can check it's source in github here.
The default command used is mongod, which can be overriden with the --auth switch;
docker run -d ... mongodb:latest mongo --auth
If you need to create the user, then we have to override the /entrypoint.sh startup script.
docker run -d ... -v $PWD/new_entrypoint.sh:/entrypoint.sh mongodb:latest
Docker Compose
mongo:
image: mongo:latest
command: ["mongod", "--auth"]
expose: "27017"
ports: "27017:27017"
volumes:
- $(cwd)/new_entrypoint.sh:/entrypoint.sh
Execute file
http://stackoverflow.com/questions/4881208/how-to-put-username-password-in-mongodb
Start mongo with auth:
$ mongod --auth
Access admin database, execute file:
$ mongod admin 'create_user.js'
db.createUser({user:"admin_name", pwd:"1234",roles:["readWrite","dbAdmin"]})
Entrypoint
The entrypoint script:
#!/bin/bash
set -e
if [ "${1:0:1}" = '-' ]; then
set -- mongod "$@"
fi
if [ "$1" = 'mongod' ]; then
chown -R mongodb /data/db
numa='numactl --interleave=all'
if $numa true &> /dev/null; then
set -- $numa "$@"
fi
exec gosu mongodb "$@"
fi
exec "$@"
We need to create a user and password:
#!/bin/bash
mongo
use admin
db.createUser({user:"$MONGO_ADMIN_USERNAME", pwd:"$MONGO_ADMIN_PASSWORD", roles:["root", "admin"]});
http://stackoverflow.com/questions/34559557/how-to-enable-authentication-on-mongodb-through-docker http://stackoverflow.com/questions/35772977/authentication-issue-in-docker-mongodb