DEV Community

Cover image for Set up MongoDB primary and secondary with Docker.
prakash chokalingam
prakash chokalingam

Posted on

Set up MongoDB primary and secondary with Docker.

MongoDB is a popular choice for developers today when it comes to NoSQL databases. The MongoDB Community Edition is readily available as a Docker image.

To enable MongoDB transactional queries, you need to configure a replica set with primary and secondary database connections.

In this post, we’ll walk through how to set up MongoDB’s primary and secondary nodes and configure a replica set using a single docker-compose file.

Prerequisites for Running the Below docker-compose File

  • Create a Docker Network
 docker network create dev_network
Enter fullscreen mode Exit fullscreen mode
  • Replica Set Configuration Script

Create a file named replicaset.sh under the scripts directory to configure the replica set:

scripts/replicaset.sh

#!/bin/bash
#MONGODB1=`ping -c 1 mongo1 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
#MONGODB2=`ping -c 1 mongo2 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
#MONGODB3=`ping -c 1 mongo3 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB1=primary
MONGODB2=secondary1
MONGODB3=secondary2
echo "**********************************************" ${MONGODB1}
echo "Waiting for startup.."
until curl http://${MONGODB1}:27017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
printf '.'
sleep 1
done
# echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1
# echo "Started.."
echo SETUP.sh time now: `date +"%T" `
mongosh --host ${MONGODB1}:27017 <<EOF
var cfg = {
"_id": "rs0",
"protocolVersion": 1,
"version": 1,
"members": [
{
"_id": 0,
"host": "${MONGODB1}:27017",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:27017",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:27017",
"priority": 0
}
],settings: {chainingAllowed: true}
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
db.getMongo().setReadPref('nearest');
db.getMongo().setSlaveOk();
rs.secondaryOk();
EOF
view raw replicaset.sh hosted with ❤ by GitHub
  • Grant Permissions to the Script Ensure the script has execute permissions so Docker can access it:
 chmod +x scripts/replicaset.sh
Enter fullscreen mode Exit fullscreen mode
  • Update the Host File for Resolvers

Map the Mongo paths to localhost by editing the /etc/hosts file:

127.0.0.1   primary secondary1 secondary2 # mongo docker resolvers
Enter fullscreen mode Exit fullscreen mode
  • Create the Docker Compose File

Define your MongoDB configuration in a docker-compose.yml file:

name: 'dev-services'
version: "3.8"
# refer https://dev.to/prakash_chokalingam/set-up-mongodb-primary-and-secondary-with-docker-7gn
mongo-replica-setup:
container_name: mongo-replica-setup
image: mongo:latest
restart: on-failure
networks:
- dev_network
volumes:
- ./scripts:/scripts
entrypoint: [ "/scripts/replicaset.sh" ]
depends_on:
- mongo-primary
- mongo-secondary1
- mongo-secondary2
mongo-primary:
hostname: primary
container_name: primarymongo
image: mongo:latest
networks:
- dev_network
expose:
- 27017
ports:
- 27017:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--dbpath", "/data/db" ]
volumes:
- ./volumes/mongo/primary/db:/data/db
- ./volumes/mongo/primary/configdb:/data/configdb
environment:
- MONGO_INITDB_DATABASE=dbname
mongo-secondary1:
hostname: secondary1
container_name: secondary1mongo
image: mongo:latest
networks:
- dev_network
expose:
- 27017
ports:
- 27018:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--dbpath", "/data/db"]
volumes:
- ./volumes/mongo/secondary1/db:/data/db
- ./volumes/mongo/secondary1/configdb:/data/configdb
# Arbiter
mongo-secondary2:
hostname: secondary2
container_name: secondary2mongo
image: mongo:latest
networks:
- dev_network
expose:
- 27017
ports:
- 27019:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--dbpath", "/data/db" ]
volumes:
- ./volumes/mongo/secondary2/db:/data/db
- ./volumes/mongo/secondary2/configdb:/data/configdb
networks:
dev_network:
name: 'dev_network'
external: true
  • Run the Docker Compose File Start the containers using the following command:
docker-compose up
Enter fullscreen mode Exit fullscreen mode

🎉 That’s it! Your MongoDB primary and secondaries are up and running. The replica set is configured and ready to use.

Your MongoDB connection URL is:

mongodb://localhost:27020,localhost:27021,localhost:27022/dbname?replicaSet=rs0&retryWrites=true&w=majority

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs