DEV Community

Mario García
Mario García

Posted on • Updated on

Deploy a MongoDB Cluster With Docker Compose

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


Enter fullscreen mode Exit fullscreen mode
  • 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


Enter fullscreen mode Exit fullscreen mode
  • 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


Enter fullscreen mode Exit fullscreen mode


$ docker run -d -p 27019:27017 --name mongo3 --network mongodbCluster mongo:6 mongod --replSet myReplicaSet --bind_ip localhost,mongo3


Enter fullscreen mode Exit fullscreen mode
  • 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\"}
 ]
})"


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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
    restart: "no"
    entrypoint: [ "bash", "/scripts/mongo_setup.sh"]


Enter fullscreen mode Exit fullscreen mode
  1. Three services are deployed, mongo1, monogo2, mongo3, and mongosetup
  2. The mongo:6 image is used as base to create the containers
  3. Hostnames are assigned to the containers that will be part of the cluster, mongo1, monogo2, and mongo3
  4. Names are assigned to the containers, mongo1, monogo2, and mongo3
  5. Ports in the host are set, and they will redirect the requests to the ports in the containers
  6. The entrypoint option is used to specify the command that will be executed once the container is initialized
  7. The mongosetup service 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


Enter fullscreen mode Exit fullscreen mode


#!/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


Enter fullscreen mode Exit fullscreen mode

The above script will initiate the replica set.

Now run the following command to deploy the cluster:



$ docker compose up --wait


Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode




Conclusion

Through this blog post, you learned how to deploy a MongoDB cluster with replication enabled, using Docker Compose.


Support me on Buy Me A Coffee

Top comments (4)

Collapse
 
funkycadet profile image
Eric Alaribe

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:

  • ./scripts:/scripts

That way, the script would actually run and the containers would be started. Thanks

Collapse
 
ekowbaah profile image
Ekow Baah-Nyarkoh

what will be the connection string for the replicas

Collapse
 
pawel-piatkowski profile image
Paweł Piątkowski

mongodb://localhost:27017/?directConnection=true will do the job

Collapse
 
khang_doan_ profile image
Khang Đoan

what if I want to connect to the whole replica set