From a previous blog post, if you want to deploy a MongoDB cluster in a local development environment, you have to follow the next steps:
- Create a Docker network
$ docker network create mongodbCluster
- Create a primary node and add it to the network created before
$ docker run -d -p 27017:27017 --name mongo1 --network mongodbCluster mongo:6 mongod --replSet myReplicaSet --bind_ip localhost,mongo1
- Once the primary node is created, the secondary nodes of the replica set must be created
$ docker run -d -p 27018:27017 --name mongo2 --network mongodbCluster mongo:6 mongod --replSet myReplicaSet --bind_ip localhost,mongo2
$ docker run -d -p 27019:27017 --name mongo3 --network mongodbCluster mongo:6 mongod --replSet myReplicaSet --bind_ip localhost,mongo3
- And finally, initiate the replica set
$ docker exec -it mongo1 mongosh --eval "rs.initiate({
_id: \"myReplicaSet\",
members: [
{_id: 0, host: \"mongo1\"},
{_id: 1, host: \"mongo2\"},
{_id: 2, host: \"mongo3\"}
]
})"
You can also use Docker Compose to deploy the cluster, without having to write all the previous commands.
Through this blog post, you will learn how to use Docker Compose for deploying a MongoDB cluster with replication enabled.
Docker Compose
First, create a project directory:
$ mkdir mongodb-cluster
Then, create a compose.yaml file with the following content:
services:
mongo1:
image: mongo:6
hostname: mongo1
container_name: mongo1
ports:
- 27017:27017
entrypoint: ["mongod", "--replSet", "myReplicaSet", "--bind_ip", "localhost,mongo1"]
mongo2:
image: mongo:6
hostname: mongo2
container_name: mongo2
ports:
- 27018:27017
entrypoint: ["mongod", "--replSet", "myReplicaSet", "--bind_ip", "localhost,mongo2"]
mongo3:
image: mongo:6
hostname: mongo3
container_name: mongo3
ports:
- 27019:27017
entrypoint: ["mongod", "--replSet", "myReplicaSet", "--bind_ip", "localhost,mongo3"]
mongosetup:
image: mongo:6
depends_on:
- mongo1
- mongo2
- mongo3
volumes:
- ./scripts:/scripts
restart: "no"
entrypoint: [ "bash", "/scripts/mongo_setup.sh"]
- Three services are deployed,
mongo1,monogo2,mongo3, andmongosetup - The
mongo:6image is used as base to create the containers - Hostnames are assigned to the containers that will be part of the cluster,
mongo1,monogo2, andmongo3 - Names are assigned to the containers,
mongo1,monogo2, andmongo3 - Ports in the host are set, and they will redirect the requests to the ports in the containers
- The
entrypointoption is used to specify the command that will be executed once the container is initialized - The
mongosetupservice will create a container that is used to initiate the replica set
Once the compose.yaml file has been created, create a scripts directory, and a BASH script (mongo_setup.sh), inside that directory, with the following content:
$ mkdir scripts
$ touch scripts/mongo_setup.sh
#!/bin/bash
sleep 10
mongosh --host mongo1:27017 <<EOF
var cfg = {
"_id": "myReplicaSet",
"version": 1,
"members": [
{
"_id": 0,
"host": "mongo1:27017",
"priority": 2
},
{
"_id": 1,
"host": "mongo2:27017",
"priority": 0
},
{
"_id": 2,
"host": "mongo3:27017",
"priority": 0
}
]
};
rs.initiate(cfg);
EOF
The above script will initiate the replica set.
Now run the following command to deploy the cluster:
$ docker compose up --wait
Your MongoDB cluster has been deployed, and it's ready to be used. If for any reason you need to remove the cluster, just run:
$ docker compose down
Connecting to the Replica Set
If you want to connect to the replica set via mongosh running on the host, add the following lines to the /etc/hosts:
127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
Then run the following command:
mongosh "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=myReplicaSet"
If you're connecting from an application running in the same Docker network, use the following connection string:
mongodb://mongo1:27017,mongo2:27017,mongo3:27017/?replicaSet=myReplicaSet
Now you're connected to the replica set.
Conclusion
Through this blog post, you learned how to deploy a MongoDB cluster with replication enabled, using Docker Compose.

Top comments (10)
I'd like to suggest an update to the Docker Compose section where you indicated that a scripts folder should be created. Either you update the docs saying that the script should just be created directly in the project folder or you map the scripts folder under volumes like so:
volumes:
That way, the script would actually run and the containers would be started. Thanks
thanks man
what will be the connection string for the replicas
Did you get the answer ? im trying to connect with mongodb://localhost:27017,localhost:27018,localhost:27019 and nothing happen. my computer just hangs
I will review the content of the post, and update as soon as possible, to answer the questions in the comments
Post updated with instructions on how to connect to the replica set
can we run everyting with
docker compose up?mongodb://localhost:27017/?directConnection=true will do the job
what if I want to connect to the whole replica set
The mongo setup container shuts down which shuts down the rest of the services.