How to enable authentication on MongoDB through Docker?

后端 未结 10 2143
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 19:07

    Just dropping a .js file into the entry point init folder works for this

    e.g. entrypoint.js

    var db = connect("mongodb://localhost/admin");
    
    db.createUser(
        {
            user: "yourAdminUserName",
            pwd: "yourAdminPassword",
            roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
        }
    )
    

    docker-compose.yml:

    db:
      image: mongo:3.2
      volumes:
       - /my/own/datadir:/data/db
       - ../mongo-entrypoint:/docker-entrypoint-initdb.d
    

    Doing the rest by hand or more of the same works.

    If you want to you can also drop a .sh file into the init folder to clean up the files so they are not laying around: zz-cleanup.sh.

提交回复
热议问题