How to create user in mongodb with docker-compose

后端 未结 8 1179
天命终不由人
天命终不由人 2020-12-23 20:28

I\'m trying to create some kind of script that will create a docker with mongodb and automatically create a user.

I can usually manage my docker images with docker-c

8条回答
  •  北海茫月
    2020-12-23 20:49

    After reading the the official mongo docker page, I've found that you can create an admin user one single time, even if the auth option is being used. This is not well documented, but it simply works (hope it is not a feature). Therefore, you can keep using the auth option all the time.

    I created a github repository with scripts wrapping up the commands to be used. The most important command lines to run are:

    docker exec db_mongodb mongo admin /setup/create-admin.js
    docker exec db_mongodb mongo admin /setup/create-user.js -u admin -p admin --authenticationDatabase admin
    

    The first line will create the admin user (and mongo will not complain even with auth option). The second line will create your "normal" user, using the admin rights from the first one.

提交回复
热议问题