first create a network:
docker network create local_containers
create an .env file to hold the Environment variables:
FORWARD_MONGODB_PORT=27017
FORWARD_MONGODB_EXPRESS_PORT=8081
MONGODB_USERNAME=M0nG0R00t
MONGODB_PASSWORD=M0nG0Pa$$
MONGODB_CLUSTER_HOSTS=mongodb1:27017,mongodb2:27017,mongodb3:27017
create a key for the cluster auth:
echo "MONGODB_CLUSTER_KEY="$(openssl rand -base64 756 | sed -z 's/\n/\\n/g') >> .env
create docker-compose.yml file:
version: "3"
services:
mongodb1:
image: blackorder/mongo-cluster
restart: always
ports:
- '${FORWARD_MONGODB_PORT:-27017}:27017'
volumes:
- mongo1_db:/data/db
- mongo1_configdb:/data/configdb
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 10s
networks:
- local_containers
environment:
MONGODB_CLUSTER_HOSTS: ${MONGODB_CLUSTER_HOSTS:?err}
MONGODB_CLUSTER_KEY: ${MONGODB_CLUSTER_KEY:?err}
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME:?err}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD:?err}
mongodb2:
image: blackorder/mongo-cluster
restart: always
volumes:
- mongo2_db:/data/db
- mongo2_configdb:/data/configdb
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 10s
networks:
- local_containers
environment:
MONGODB_CLUSTER_KEY: ${MONGODB_CLUSTER_KEY:?err}
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME:?err}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD:?err}
mongodb3:
image: blackorder/mongo-cluster
restart: always
volumes:
- mongo3_db:/data/db
- mongo3_configdb:/data/configdb
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 10s
networks:
- local_containers
environment:
MONGODB_CLUSTER_KEY: ${MONGODB_CLUSTER_KEY:?err}
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME:?err}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD:?err}
mongo-express:
image: mongo-express:latest
restart: always
ports:
- '${FORWARD_MONGODB_EXPRESS_PORT:-8081}:8081'
networks:
- local_containers
depends_on:
mongodb1:
condition: service_healthy
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGODB_USERNAME:?err}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGODB_PASSWORD:?err}
ME_CONFIG_MONGODB_URL: 'mongodb://${MONGODB_USERNAME:?err}:${MONGODB_PASSWORD:?err}@${MONGODB_CLUSTER_HOSTS:?err}/?replicaSet=rs'
ME_CONFIG_MONGODB_ENABLE_ADMIN: 'true'
ME_CONFIG_BASICAUTH_USERNAME: ${MONGODB_USERNAME:?err}
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGODB_PASSWORD:?err}
networks:
local_containers:
external: true
volumes:
mongo1_db: {}
mongo1_configdb: {}
mongo2_db: {}
mongo2_configdb: {}
mongo3_db: {}
mongo3_configdb: {}
Start the cluster:
docker compose up -d
Content type
Image
Digest
sha256:2ebfd80b5…
Size
248.1 MB
Last updated
over 2 years ago
docker pull blackorder/mongo-cluster