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>