Create a new user for MongoDB inside Docker

I'm using the default MongoDB Docker image and I'm trying to create a new user for the database.

I'm currently trying to do it this way:


MAINTAINER <alexandernst>

ADD create_ddbb.js /tmp/

RUN mongod --fork --logpath /var/log/mongodb.log \
    && sleep 5 && mongo foobar /tmp/create_ddbb.js 

And the create_ddbb.js:

      user: "*******",
      pwd: "*******************",
      roles: [
         { role: "readWrite", db: "foobar" }

And when I build the Dockerfile, I see:

Step 4 : RUN mongod --fork --logpath /var/log/mongodb.log       && sleep 5 && mongo foobar /tmp/create_ddbb.js
 ---> Running in 58ba44d02508
about to fork child process, waiting until server is ready for connections.
forked process: 9
child process started successfully, parent exiting
MongoDB shell version: 3.2.6
connecting to: foobar
Successfully added user: {
        "user" : "***********",
        "roles" : [
                        "role" : "readWrite",
                        "db" : "foobar"
 ---> e73b6c8c8b83
Removing intermediate container 58ba44d02508
Successfully built e73b6c8c8b83

so the user is created, but then when I try to connect, I get:

mongo_1  | 2016-05-13T17:44:02.159+0000 I NETWORK  [initandlisten] connection accepted from #1 (1 connection now open)
mongo_1  | 2016-05-13T17:44:02.160+0000 I ACCESS   [conn1] SCRAM-SHA-1 authentication failed for ********* on foobar from client ; UserNotFound: Could not find user *********@foobar
mongo_1  | 2016-05-13T17:44:02.160+0000 I NETWORK  [conn1] end connection (0 connections now open)

Why is that happening? How can I persist the created user?

My solution:

Inside your Dockerfile:

ADD create_ddbb.js /tmp/

RUN mongod -f /etc/mongod.conf --fork --logpath /var/log/mongodb.log \
    && sleep 5 \
    && mongo <YOUR DATABASE> /tmp/create_ddbb.js

Inside the create_ddbb.js:

      user: "your_user",
      pwd: "********************",
      roles: [
         { role: "dbOwner", db: "your_database" }
        w: "majority",
        wtimeout: 5000

The createColleciton("test") at the end is extremely important. Without that, the createUser isn't applied. I don't know why exactly, sorry.

the user should be added in the admin database so like this: RUN mongod --fork --logpath /var/log/mongodb.log \ && sleep 5 && mongo foobar /tmp/create_ddbb.js

I didn't use the Dockerfile. Instead I used docker-compose.yml to specify the container. Below is the content of docker-compose.yml

version: "3.7"
    container_name: container-mongodb
    image: mongo:latest
    restart: always
      - 27017:27017

  - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

Create a directory docker-entrypoint-initdb.d and create mongo-init.js file. and put the below code to add user.

print("Started adding the users.");
db = db.getSiblingDB("admin");
  user: "userx",
  pwd: "1234",
  roles: [{ role: "readWrite", db: "admin" }],
print("End adding the user roles.");

Then run docker compose up from the directory where you have kept the docker compose.yml. The file tree should look like:

 ┣ docker-entrypoint-initdb.d
 ┃ ┗ mongo-init.js
 ┗ docker-compose.yaml

Check for the logs if you encounter any errors.

I could not make the @alexandernst's solution run. In combination with I just had to create my Dockerfile so and it wored.

COPY create_ddbb.js /docker-entrypoint-initdb.d/

The create_ddbb.js was similar to @alexandernst's, but without the last line which created a test collection.