Resetting admin password for a container running MongoDB

For unknown reasons I cannot connect as admin to my self-hosted MongoDB running in a docker container. This container is a standard 6.0 container launched as part of a group with docker compose. The following is the relevant part of docker-compose.yml:

mongo_mema:
    image: "mongo:6.0"
    command: ["--auth"]
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - "${MPORT}:${MPORT}"
    volumes:
      - mongo_mema_data:/data/db
    networks:
      - memanet

and the .env file contains (passwords are fictitious):

SERV_MDB=mongo_mema
MPORT=27017
MONGODB_SUGGESTIONCACHE_COLLECTION=suggestioncache
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=-MyLabradorFartsUnderMyDesk

I tried the following from the same directory where .env and docker-compose.yml are:
docker compose restart mongo_mema
and would have expected to be able to use the following connection URL:
mongo://admin:-MyLabradorFartsUnderMyDesk@localhost:27017/
from within the running container (or the host since 27017 is exposed on the host too) but the authentication fails.
This container is running a database in use so I need to minimize downtimes.
What error am I doing?
Thanks a lot.

You already know the procedure per your other thread you just need to execute it correctly.

In this instance I would stop the container and the start a new one on the command line using the --volumes-from argument to start a mongod without --auth

docker run --name resetMongoPW --rm -d --volumes-from projectName_mongo_mema mongo:6.0

docker exec resetMongoPW mongosh --quiet --eval --quiet 'db.getSiblingDB("admin").changeUserPassword("admin","password")'

These are only for the initial db, they won’t update after the initialisation.

MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD
1 Like

Thanks Chris I appreciate it. Very godd to know those envs are only for the initial setup, I had overlooked that.

Never used the --volumes-from option. Shall study a bit. From your example not sure what object you’re referring to with the string projectName_mongo_mema . The volume I use for persistance as per the docker-compose.yml file is mongo_mema_data

docker volume ls gives:

local     mema_services_mongo_mema_data
local     mema_services_mongodb_data
local     mongodb_data

Take care.