0

I cannot figure out how to connect to MongoDB using secrets in Docker Compose.

docker-compose.yml

version: '3.4'
services:
  mongo-container:
    network_mode: host
    image: mongo:6.0.5
    environment:
      MONGO_INITDB_ROOT_PASSWORD_FILE: /run/secrets/mongodb_root_password
      MONGO_INITDB_ROOT_USERNAME_FILE: /run/secrets/mongodb_root_username
    secrets:
      - mongodb_root_password
      - mongodb_root_username
    volumes:
      - "/storage/docker/mongodb:/data/db"
    command: mongod
secrets:
  mongodb_root_password:
    file: /home/mongodb/.mongodb_root_password
  mongodb_root_username:
    file: /home/mongodb/.mongodb_root_username

I run the container from the same directory that docker-compose.yml is in using docker-compose up -d.

This works
mongodb://admin:pass@host:27017/?authMechanism=DEFAULT

Using the credentials in my secrets files does not work
mongodb://<username>:<password>@host:27017/?authMechanism=DEFAULT

$ cat /home/mongodb/.mongodb_root_password
<password>
$ cat /home/mongodb/.mongodb_root_username
<username>
Harry Stuart
  • 1,781
  • 2
  • 24
  • 39
  • Maybe this one: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jul 10 '23 at 06:21
  • I've tried setting the auth database to `admin` with no luck. I am not even trying to add a user, I am just trying to change the creds for the root user - who can login using defaults. I just don't seem to be able to override them. – Harry Stuart Jul 10 '23 at 06:33

0 Answers0