Skip to content

azita-abdollahi/mongodb-replicaset-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Replica Set with Docker

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


📦 Requirements

  1. 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
  2. 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
  1. Environment variables: Copy the example and edit as needed:

  2. cp .env.example .env

🚀 Getting Started

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


🔧 Services Overview

Service Role Port
mongodb1 Primary 27017
mongodb2 Secondary 27017
mongodb3 Secondary 27017
mongo-express Web UI 8081
mongo-init Init script

📖 Documentation

Full setup, TLS configuration, replication testing, and troubleshooting guides:

📚 Wiki Home »


📌 Notes

  • This setup uses keyFile authentication (no TLS by default).
  • MongoDB 8.0+ requires TLS for certain modes (e.g., sendKeyFile).

About

setup mongodb replicaset in docker and mongo-express

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages