How to enable authentication on MongoDB through Docker?

后端 未结 10 2145
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 18:53

I want to spin-up a docker for mongodb:latest but allow only certain user(s) to access certain db(s) (i.e. enable --auth). No one else should acces

10条回答
  •  不知归路
    2020-11-29 19:12

    you can use env variables to setup username and password for mongo

    MONGO_INITDB_ROOT_USERNAME
    MONGO_INITDB_ROOT_PASSWORD
    

    using simple docker command

    docker run -e MONGO_INITDB_ROOT_USERNAME=my-user MONGO_INITDB_ROOT_PASSWORD=my-password mongo
    

    using docker-compose

    version: '3.1'
    
    services:
    
      mongo:
        image: mongo
        restart: always
        environment:
          MONGO_INITDB_ROOT_USERNAME: my-user
          MONGO_INITDB_ROOT_PASSWORD: my-password
    

    and the last option is to manually access the container and set the user and password inside the mongo docker container

    docker exec -it mongo-container bash
    

    now you can use mongo shell command to configure everything that you want

提交回复
热议问题