This repository sets up a MongoDB replica set using Docker Compose. It includes:
- Three MongoDB nodes (Primary, Secondary, Secondary)
- Replica set initialization script
- Mongo Express for UI-based DB access
🔗 If you want to load balance reads across secondaries, check out the companion project:
👉 haproxy-mongodb-balancer
-
Generate the shared replica set key file (for internal authentication):
openssl rand -base64 756 > replica.key sudo chown 999:999 replica.key chmod 600 replica.key
-
Create Docker network (shared across services):
docker network create \
--driver=bridge \
--subnet=192.168.22.0/24 \
--ip-range=192.168.22.0/24 \
--gateway=192.168.22.254 \
connet
-
Environment variables: Copy the example and edit as needed:
-
cp .env.example .env
docker compose up -d
docker compose ps
# down containers with volumes
docker compose down
Replica set will auto-init via mongo_rs_init.sh
once all nodes are healthy.
Mongo Express will be available at http://localhost:8081
Service | Role | Port |
---|---|---|
mongodb1 | Primary | 27017 |
mongodb2 | Secondary | 27017 |
mongodb3 | Secondary | 27017 |
mongo-express | Web UI | 8081 |
mongo-init | Init script | — |
Full setup, TLS configuration, replication testing, and troubleshooting guides:
- Setup Instructions
- Mongo Express & Replica Set
- TLS Configuration (Recommended)
- Replica Set Testing & Failover
- Troubleshooting
- This setup uses keyFile authentication (no TLS by default).
- MongoDB 8.0+ requires TLS for certain modes (e.g.,
sendKeyFile
).