mongodbauthenticationdocker-compose

How to change mongodb password in docker compose


I tried to change the mongodb password in docker-compose.yaml file directly by changing the - MONGO_INITDB_ROOT_PASSWORD parameter within environment in mongodb service.

Here is my docker-compose.yaml file before changing the password:

mongo-dev:
 container_name: mongo-dev
 image: mongo
 restart: unless-stopped
 environment:
  - AUTH=yes
  - MONGO_INITDB_ROOT_USERNAME=root
  - MONGO_INITDB_ROOT_PASSWORD=old-pass
 volumes:
  - /data/mongodb-dev:/data/db
 ports:
  - 27017:27017

I changed MONGO_INITDB_ROOT_PASSWORD value from "old-pass" to "new-pass" and used docker-compose up -d command to re-create mongodb container:

mongo-dev:
 container_name: mongo-dev
 image: mongo
 restart: unless-stopped
 environment:
  - AUTH=yes
  - MONGO_INITDB_ROOT_USERNAME=root
  - MONGO_INITDB_ROOT_PASSWORD=new-pass
 volumes:
  - /data/mongodb-dev:/data/db
 ports:
  - 27017:27017

And when i tried to connect to DB with "new-pass" as password i got authentication error but it still connecting to mongodb container with "old-pass".

It seems to me that changing the MONGO_INITDB_ROOT_PASSWORD in docker-compose file does not apply password changing in mongodb container.


Solution

  • I found out the correct way to change mongodb password. These following steps will help: