Friday, April 6, 2018

MongoDB authentication through Docker

From Stackoverflow

If you take a look at:
you will notice that there are two variables used in the docker-entrypoint.sh:
  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD
You can use them to setup root user. For example you can use following docker-compose.yml file:
docker-compose.yml
version: '2'
services:
  mongodb:
  image: mongo:3.4.2
  environment:
      # provide your credentials here
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=rootPassXXX
  ports:
    - "27017:27017"
  volumes:
    - mongodb:/data/db
      # if you wish to setup additional user accounts specific per DB or with different roles you can use following entry point
    - "$PWD/mongo-entrypoint/:/docker-entrypoint-initdb.d/"
  # no --auth is needed here as presence of username and password add this option automatically
  command: mongod

No comments:

Post a Comment