I have a mongo container created in Compose:
version: '2' volumes: mongodata: driver: local services: mongo: image: mongo:latest hostname: ${MONGODB_HOST} restart: always ports: - "27017:27017" volumes: - mongodata:/data/db
This works fine, however now I want to put the password in the database. To do this, firstly, I understand that I need to create a database, add a password, and then restart it using the --auth flag. My question is how to make this process using docker-compose.
I can do this if I do everything without a docker essay. The problems that I see when creating are as follows:
a) Work in the docker is performed within the docker network. b) docker-compose cannot run different commands at the beginning, as during production. - this is important because although some people say that you can run --auth at the beginning and this will allow you to set a password for the first time, this does not seem to be the case.
One of the solutions I started working on was a shell script that I would run on all my servers before running the docker-compose file:
This file creates a temporary container, sets the username / password on it, stops the original container, creates a new one using the old volume container, and deletes the original one. The new mongo container now has a password.
So my final question is: what's the best way to do this in docker-compose?
My other containers in my docker file should have access to mongo, so I think the volume container containing the mongo data should be on the same network as docker-compose creates
source share