Skip to content

Instantly share code, notes, and snippets.

@ftmoose
Last active February 17, 2024 10:30
Show Gist options
  • Save ftmoose/2b4481d99669994e0767af3e79b3e869 to your computer and use it in GitHub Desktop.
Save ftmoose/2b4481d99669994e0767af3e79b3e869 to your computer and use it in GitHub Desktop.
MongoDB ReplicaSet Docker Configuration

Directory Structure

app-root
  -> (src)
  -> docker-compose.yml
  -> scripts
    -> setupReplicaSet.sh

Map Hostnames

Add the following to your /etc/hosts file in order to connect to the replica set locally

127.0.0.1   mongo1
127.0.0.1   mongo2
127.0.0.1   mongo3

Start up

$ sudo chmod 755 ./scripts/setupReplicaSet.sh
$ sudo docker-compose.yml up -d

Connection URI

Based on the hostnames and ports in the docker-compose.yml and setupReplicaSet.sh files

mongodb://mongo1:27017,mongo2:27018,mongo3:27019/<DB-NAME-HERE>?replicaSet=rs0
version: "3.1"
# Our containers
services:
# Temporary container used to initialize the replica set
mongo-setup:
container_name: mongo-setup
image: mongo
restart: on-failure
networks:
default:
volumes:
- ./scripts:/scripts # Ensure the './scripts' directory exists with the 'setupReplicaSet.sh' file
entrypoint: ["/scripts/setupReplicaSet.sh"] # Call the 'setupReplicaSet.sh' script on container init
depends_on: # Wait for the 3 DB containers to spin up first
- mongo1
- mongo2
- mongo3
mongo1:
hostname: mongo1 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file
container_name: mongo1
image: mongo
expose:
- 27017
ports:
- 27017:27017
networks:
default:
restart: always
volumes:
- ./mongo-volumes/mongo1/data/db:/data/db # Create a volume to persist data
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27017"]
mongo2:
hostname: mongo2 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file
container_name: mongo2
image: mongo
expose:
- 27018
ports:
- 27018:27018
networks:
default:
restart: always
volumes:
- ./mongo-volumes/mongo2/data/db:/data/db # Create a volume to persist data
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27018"]
mongo3:
hostname: mongo3 # You need to map this hostname to 127.0.0.1 in your /etc/hosts file
container_name: mongo3
image: mongo
expose:
- 27019
ports:
- 27019:27019
networks:
default:
restart: always
volumes:
- ./mongo-volumes/mongo3/data/db:/data/db # Create a volume to persist data
entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0", "--journal", "--dbpath", "/data/db", "--enableMajorityReadConcern", "false", "--port", "27019"]
#!/bin/bash
REPLICA_SET_NAME=rs0
MONGODB1=mongo1
MONGODB1_PORT=27017
MONGODB2=mongo2
MONGODB2_PORT=27018
MONGODB3=mongo3
MONGODB3_PORT=27019
echo "********** setupReplicaSet.sh **********"
echo ${MONGODB1}
until curl http://${MONGODB1}:${MONGODB1_PORT}/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
printf '.'
sleep 1
done
echo setupReplicaSet.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:${MONGODB1_PORT} <<EOF
var cfg = {
"_id": "${REPLICA_SET_NAME}",
"members": [
{
"_id": 0,
"host": "${MONGODB1}:${MONGODB1_PORT}",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:${MONGODB2_PORT}",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:${MONGODB3_PORT}",
"priority": 0
}
]
};
rs.initiate(cfg);
rs.reconfig(cfg, {force: true});
rs.secondaryOk();
rs.conf();
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment