简体   繁体   中英

Mongo docker image not persisting user creation

I am trying to build my own mongo docker image. After installing mongo in the build process, I also want to add few pre-seeded users.

db = db = db.getSiblingDB("<dbname>");

db.createUser(
   {
     user: "<username>",
     pwd: "<password>",
     roles: [ { role: "readWrite", db: "<dbname>" } ]
   }
)

Once the image is built , I spin up a container and "use" correct db , I do not see the users created --

db.getUsers() [ ]

I have read the article here , which suggests to create ephemeral containers but does not necessary claim that they are indeed ephemeral.

Any insight would help.

I can tell you from vast experience with Mongo and Docker (or any database for that matter); that is is better not to make an image with a database embedded.

It can be done but one some platforms you can lose a lot of time getting it up to speed.

Recommendation: Keep you code and database on separate servers and bake in the DB credentials in your Docker image.

Host DB for free here https://mlab.com/

This is just my opinion please take many opinions and formulate your own. More reading here: https://vsupalov.com/database-in-docker/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM