0

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?

Dumb
  • 89
  • 2
  • Which documentation you are following? – D. SM Jul 25 '20 at 19:46
  • Hey, I did not follow any documentation. Just I look the official mongoDB docker hub page and then created the .sh files and tried to load it but it is ignoring the entrypoint file. I am able to login with root. – Dumb Jul 25 '20 at 22:12
  • I also looked here https://stackoverflow.com/questions/34559557/how-to-enable-authentication-on-mongodb-through-docker/34562109#34562109 and here is the script used for mongo-init.sh https://stackoverflow.com/questions/42912755/how-to-create-a-db-for-mongodb-container-on-start-up – Dumb Jul 25 '20 at 22:18
  • So what have you gotten working? Everything works without auth? You know where the server logs are? – D. SM Jul 27 '20 at 06:13
  • Yes, the problem is that I am not able to create an application user in the MongoDB, admin is created and working fine but the application user specified in the mongo-it.sh is not created? – Dumb Jul 27 '20 at 08:00

0 Answers0