Authentication on dockerized mongodb

9,686

Solution 1

The MongoDB manual includes tutorials for enabling authentication and adding a user to a database.

Assuming you are running a standalone image called mongo, you can pass the parameter to enable authentication by starting your image with a docker command line similar to:

 docker run --name mongod -d mongo --auth

The rest of the steps for enabling and configuring access control are not specific to Docker. After starting mongod with the --auth parameter you need to create an admin user and then any additional users required.

It's also worth reviewing the MongoDB Security Checklist for other security considerations.

Note: MongoDB 2.6 reached end of life in October, 2016. I strongly encourage upgrading to a supported version (eg. latest 3.2.x or 3.4.x) for continued support.

Solution 2

  1. Run the mongo docker instance on your server

    docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo
    
  2. Open bash on the running docker instance.

    docker ps
    

    CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS NAMES

    b07599e429fb mongo "docker-entrypoint..." 35 minutes ago Up 35 minutes 0.0.0.0:27017->27017/tcp musing_stallman

    docker exec -it b07599e429fb bash
    root@b07599e429fb:/#
    
  3. Enter the mongo shell by typing mongo.

    root@b07599e429fb:/# mongo
    
  4. For this example, I will set up a user named ian and give that user read & write access to the cool_db database.

    > use cool_db
    
    > db.createUser({
        user: 'ian',
        pwd: 'secretPassword',
        roles: [{ role: 'readWrite', db:'cool_db'}]
    })
    
  5. Exit from mongod shell and bash.

  6. Now run the mongo docker with auth enabled.

    docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo mongod --auth
    
  7. I was able to connect to the instance running on a Google Cloud server from my local windows laptop using the below command.

    mongo <ip>:27017/cool_db -u ian -p secretPassword
    
  8. You can commit an image of the container instance. This will store all changes made to its file system including the /data/db directory. You can run this image anywhere docker-engine is running.

    docker commit mongodb mongodb-backup
    

    Reference: https://blog.madisonhub.org/setting-up-a-mongodb-server-with-auth-on-docker/

The answer is based on my answer in stackoverflow: https://stackoverflow.com/a/46645243/3877642

Share:
9,686

Related videos on Youtube

Jordi
Author by

Jordi

Updated on September 18, 2022

Comments

  • Jordi
    Jordi almost 2 years

    I'm using the official mongodb:2.6 docker image. I'm trying to figure out how to enable straightforward authentication using user:password mode.

    How do I specify mongo must run under authentication?

    How do I create each user:password?

  • Jordi
    Jordi over 7 years
    Thanks Stennie. I've got it. I thought there might be a way in order to set up a dockerized mongodb instance with users. I really might be usefull.
  • Sankaran Srinivasan
    Sankaran Srinivasan over 6 years
    Is it incorrect to refer to an already keyed in material, which could be the answer for the seeker's question?
  • Sankaran Srinivasan
    Sankaran Srinivasan over 6 years
    Shouldn't I refer to the stackoverflow links either?
  • Jenny D
    Jenny D over 6 years
    Welcome to Server Fault! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • Sankaran Srinivasan
    Sankaran Srinivasan over 6 years
    Edited the answer accordingly