replica Set mongo docker-compose
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Building a MongoDB replica set using Docker Compose is an efficient way to streamline development, testing, and deployment of highly available MongoDB architectures in a containerized environment. Replica sets are the cornerstone of MongoDB's replication strategy, offering redundancy, automatic failover, and increased data availability across several data nodes.
In this article, we’ll delve into setting up a MongoDB replica set using Docker Compose, explore the technical intricacies involved, and provide examples to illustrate the process.
Technical Overview of MongoDB Replica Set
A MongoDB replica set consists of two or more nodes, defined as:
- Primary Node: Handles all write operations and serves read requests by default.
- Secondary Nodes: Replicate the data of the primary node and can optionally handle read operations.
- Arbiter Node (Optional): Participates in elections but does not store data; useful for maintaining quorum in a replica set with an even number of members.
Docker Compose Setup
Docker Compose simplifies the use of multi-container Docker applications by allowing for declarative infrastructure setup. Below we demonstrate a docker-compose.yml
configuration for a basic MongoDB replica set of three nodes.
- 27017:27017
- mongo_net
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_REPLICA_SET_NAME=rs0
- 27018:27017
- mongo_net
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_REPLICA_SET_NAME=rs0
- 27019:27017
- mongo_net
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_REPLICA_SET_NAME=rs0
- Services: We define three services, each running a MongoDB instance:
mongo1,mongo2, andmongo3. - Image: All services use the MongoDB 5.0 image.
- Ports: Map different external ports to MongoDB's default internal port (
27017). - Environment Variables:
MONGO_INITDB_ROOT_USERNAMEandMONGO_INITDB_ROOT_PASSWORDset credentials for the root user.MONGO_REPLICA_SET_NAMEspecifies the name of the replica set.
docker exec -it mongo1 mongo: Connects to the running MongoDB containermongo1.--eval 'rs.initiate(...)': Initiates the replica set configuration within the MongoDB shell:- _id: The replica set name (
rs0). - members: Defines each member's identifier (
_id) and networkhostdetails.
- Simplification: Automates setup and configuration, reducing manual errors.
- Testing: Replicate production-like environments locally for testing.
- Scalability: Easily scale the number of nodes by adjusting the configurations.
- Portability: Transfer configurations across environments seamlessly.
- This command provides access to the cluster, and the replica set ensures that all write operations are directed to the primary node, with failover support in case the primary fails.
Related reading
- Replication Controller VS Deployment in Kubernetes
- Repository is not signed in docker build
- Resolve container name in extra_hosts option in docker-compose
- Restart container within pod
- replicas in replication
- replicate data from a realtime table to another table in SQL Server 2008R2?
- Representing graphs data structure in Python
- Reproducible results in Tensorflow with tf.set_random_seed

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.