0

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.

2 Answers2

0

You have mentioned the below property and due to this property, it is not picking your new password:

restart: unless-stopped

To ensure that the changes take effect, you need to stop the container using

docker-compose down 

and then start it again using

docker-compose up -d
Maninder
  • 1,539
  • 1
  • 10
  • 12
  • Thank you for responding, but it did not work and i found out using `db.changeUserPassword("username", "newPassword")` will work. – Saeed Pakdelnia May 09 '23 at 08:42
0

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

  • enter to mongo container bash by docker exec -it mongo-dev bash
  • enter mongo admin -u root -p old-pass command for authentication and enter to mongo shell
  • use admin db by use admin
  • enter db.changeUserPassword("root", "new-pass") command to change password