Credentials with docker compose

Currently, this’s mongodb service in docker-compose file:

mongodb:
image: mongo:4
hostname: mongo.myrepl
container_name: mongodb52
networks:
- cloud-network
command:
- --storageEngine
- wiredTiger
volumes:
- mongo-data:/data/db
restart: always
ports:
- "27017:27017"

mongodb is running without credentials. I modified to configure credentials:

 mongodb:
    image: mongo:4
    hostname: mongo.myrepl
    container_name: mongodb52
    networks:
      - cloud-network
    command:
      - --storageEngine
      - wiredTiger
      - --auth
    environment:
      - "MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USERNAME}"
      - "MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}"
      - MONGO_INITDB_DATABASE=openhab
    volumes:
      - mongo-data:/data/db
      - ./mongodb/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    restart: always
    ports:
      - "27017:27017"

with MONGO_ROOT_USERNAME: admin, MONGO_ROOT_PASSWORD: pwdadmin
mongo-init.js:

db.createUser(
    {
        user: "admin",
        pwd: "AuraSeltec",
        roles: [
            {
                role: "readWrite",
                db: "openhab"
            }
        ]
    }
);

The problem occurs when i connect to mongodb with error is authentication failed.
This’s my command: mongosh --port 27017 -u admin -p pwdadmin
I’m confusing that i use volume in docker-compose so the configuration of /data/db is configured.

Hi @Tran_Phuong and welcome to MongoDB community forums!!

To run MongoDB using docker container, I tried the following below docker compose file to enable the authentication.

version: "3.9"
services:
  mongodb:
    container_name: testMongoDB
    image: mongo:latest
    restart: always
    command:
      - --storageEngine
      - wiredTiger
      - --auth
    environment:
      - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME}
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD}
      - MONGO_INITDB_DATABASE=${MONGO_INITDB_DATABASE}
    ports:
      - "27017:27017"
    volumes:
      - ./mongodb-data:/data/db
      - ./db/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
    env_file: .env

and the .env file as:

#MongoDB Default Setup
MONGO_INITDB_ROOT_USERNAME=admin
MONGO_INITDB_ROOT_PASSWORD=password
MONGO_INITDB_DATABASE=test

I tried to log in into the the mongo shell using

aasawari.sahasrabuddhe@M-C02DV42LML85 ~ % docker exec -it 406ebdb0b484 bash
root@406ebdb0b484:/# mongosh -u admin -p password
Current Mongosh Log ID:	65671095371b0192f0100d5c
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.0
Using MongoDB:		7.0.4
Using Mongosh:		2.1.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
   The server generated these startup warnings when booting
   2023-11-29T10:14:13.759+00:00: vm.max_map_count is too low
------
test>

You can try using the above docker file and confirm if you are successfully able to make the connection.

Based on the above credentials, can you confirm the source from the where are values are being picked up in the docker compose file?

Can you confirm if you are using the above command to connect inside the docker container?

Regards
Aasawari

Hello! i’m needing execute docker compose to use it with nestjs but when i try logging add user and password defined on my docker compose and I try to make a request it tells me that I do not have permissions , just may resolved add to user to container console db.createUser … Is there a way to make this automatic, I thought that by adding MONGO_INITDB_ROOT_USERNAME this user could do everything but that is not the case.

Regards

If u put the credentials without the .env file, u connect perfectly