I am using Docker Swarm mode on my VPS solely for the purpose of using docker secrets.
The purpose of using docker secrets (limited with swarm mode) is obviously to not have any sensitive data on the localhost. With that said, I wanted to go outside the swarm network and directly bind ports to the host’s network.
Example docker-compose.yml as follows:
services:
btc_mongodb:
image: mongo:8.0.9-noble
deploy:
restart_policy:
condition: any
delay: 5s
max_attempts: 3
window: 120s
environment:
MONGO_INITDB_ROOT_USERNAME_FILE: /run/secrets/mongo_root_username
MONGO_INITDB_ROOT_PASSWORD_FILE: /run/secrets/mongo_root_password
ports:
- target: 27017
published: 27017
protocol: tcp
mode: host
command: ["mongod", "--bind_ip", "0.0.0.0", "--maxConns", "10000"]
volumes:
- db:/data/db
secrets:
- mongo_root_username
- mongo_root_password
networks:
- mongodb_network
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 30s
timeout: 10s
retries: 3
networks:
mongodb_network:
driver: overlay
attachable: true
volumes:
db:
name: mongodb
secrets:
mongo_root_username:
external: true
mongo_root_password:
external: true
# The following snippet is the port settings to directly link with the host
ports:
- target: 27017
published: 27017
protocol: tcp
mode: host