Mongo: Set up authentication fails.

Created on 1 Aug 2015  路  5Comments  路  Source: docker-library/mongo

It would be nice to have env variables for authentication enabling, to set username/password, storage engine .. etc.
For guideline I have been using https://github.com/tutumcloud/mongodb/tree/master/2.6
Here is my dockerfile:

FROM mongo:2.6

RUN mongo admin --eval "db.createUser({user: 'myuser', pwd: 'mypass', roles:[{role:'dbOwner',db:'admin'}]});"

The build returns:

MongoDB shell version: 2.6.10
connecting to: admin
2015-08-01T00:59:42.877+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-08-01T00:59:42.878+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

Expected result - to build the image and set up new user.

Most helpful comment

Failed to connect to 127.0.0.1:27017

You are getting this error because it takes a bit of time to start up mongo service. It seems people are using something like the following to avoid it:
Dockerfile:

FROM mongo:3.2.0
COPY setup.sh /setup.sh
RUN /setup.sh

setup.sh:

#!/bin/bash

mongod --dbpath /data/db &

RET=1
while [[ RET -ne 0 ]]; do
    echo "=> Waiting for confirmation of MongoDB service startup"
    sleep 5
    mongo admin --eval "help" >/dev/null 2>&1
    RET=$?
done

ADMINUSER=user
ADMINPASS=pass

echo "=> Creating an admin user in MongoDB"
mongo admin --eval "db.createUser({user: '$ADMINUSER', pwd: '$ADMINPASS', roles:[{role:'root',db:'admin'}]});"

mongod --dbpath /data/db --shutdown

However, after successfully building this image, and starting a container using this image with command --auth I can't seem to be able to connect to the database using user I created above. Anybody has idea why?

@tianon , your input would be very appreciated!

All 5 comments

I agree would great to have a variable i could set to enable auth.

Failed to connect to 127.0.0.1:27017

You are getting this error because it takes a bit of time to start up mongo service. It seems people are using something like the following to avoid it:
Dockerfile:

FROM mongo:3.2.0
COPY setup.sh /setup.sh
RUN /setup.sh

setup.sh:

#!/bin/bash

mongod --dbpath /data/db &

RET=1
while [[ RET -ne 0 ]]; do
    echo "=> Waiting for confirmation of MongoDB service startup"
    sleep 5
    mongo admin --eval "help" >/dev/null 2>&1
    RET=$?
done

ADMINUSER=user
ADMINPASS=pass

echo "=> Creating an admin user in MongoDB"
mongo admin --eval "db.createUser({user: '$ADMINUSER', pwd: '$ADMINPASS', roles:[{role:'root',db:'admin'}]});"

mongod --dbpath /data/db --shutdown

However, after successfully building this image, and starting a container using this image with command --auth I can't seem to be able to connect to the database using user I created above. Anybody has idea why?

@tianon , your input would be very appreciated!

I also think it would be great to have a variable I could set to enable auth.

@xgdgsc, like https://github.com/docker-library/mongo/pull/145? (Still fixing bugs added by this, which is why it is not documented).

Closing old issue, fixed by #145. Feel free to comment if you feel this was closed in error.

Was this page helpful?
0 / 5 - 0 ratings