Can anyone please guide me what is wrong with my approach to enable authentication on MongoDB using docker.
Docker version 19.03.8, build afacb8b7f OS ubuntu 18.04
So basically, I created mongo-it.sh files which looks like this:
mongo -- "$MONGO_INITDB_DATABASE" <<EOF
var rootUser = '$MONGO_INITDB_ROOT_USERNAME';
var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
var admin = db.getSiblingDB('admin');
admin.auth(rootUser, rootPassword);
var user = '$MONGO_INITDB_USERNAME';
var passwd = '$MONGO_INITDB_ROOT_PASSWORD';
db.createUser({user: '$MONGO_USERNAME', pwd: '$MONGO_PASSWORD', roles: ["readWrite"]});
EOF
I try to run this script with docker as follows:
docker run --name mongodDb_authn -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME="admin2" -e MONGO_INITDB_ROOT_PASSWORD="v3jpurNRZVpzzK27" -e MONGO_INITDB_DATABASE="mydb" -e MONGO_USERNAME="mongodb-user" -e MONGO_PASSWORD="v3jpurNRZVp2132" -v $PWD/mongo-init.sh:/docker-entrypoint.initdb.d/mongo-init.sh:ro mongo:4.2.8
It starts the docker but does not run the script and the output is as follows:
Successfully added user: {
"user" : "admin2",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
2020-07-25T11:52:29.004+0000 I SHARDING [ftdc] Marking collection local.oplog.rs as collection version: <unsharded>
2020-07-25T11:52:29.005+0000 E - [main] Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: No such file or directory
2020-07-25T11:52:29.007+0000 I NETWORK [conn2] end connection 127.0.0.1:34098 (0 connections now open)
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
It ignores the ''ignoring /docker-entrypoint-initdb.d'', I can authenticate with root username and password but not with another user name. The script is not executed by and ignored. Please can anyone help me with this what is the problem?