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 acces
Just dropping a .js file into the entry point init folder works for this
e.g. entrypoint.js
var db = connect("mongodb://localhost/admin");
db.createUser(
{
user: "yourAdminUserName",
pwd: "yourAdminPassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
docker-compose.yml:
db:
image: mongo:3.2
volumes:
- /my/own/datadir:/data/db
- ../mongo-entrypoint:/docker-entrypoint-initdb.d
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.