Skip to content

Replica Set Using Docker

YAML
version: "3.7"
services:

  mongo1:
    image: mongo:4.2.6
    command: "--replSet durbok-replica --config /etc/mongo/mongod.conf"
    logging:
      driver: "json-file"
      options:
        max-size: "200m"
        max-file: "10"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
      - MONGO_INITDB_DATABASE=testdb
    ports:
      - 27020:27017
    volumes:
      - ./mongodb1:/data/db
      - ./mongocfg1:/etc/mongo
    container_name: mongo1

  mongo2:
    image: mongo:4.2.6
    command: "--replSet durbok-replica --config /etc/mongo/mongod.conf"
    logging:
      driver: "json-file"
      options:
        max-size: "200m"
        max-file: "10"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
      - MONGO_INITDB_DATABASE=testdb
    ports:
      - 27021:27017
    volumes:
      - ./mongodb2:/data/db
      - ./mongocfg2:/etc/mongo
    container_name: mongo2

  mongo3:
    image: mongo:4.2.6
    command: "--replSet durbok-replica --config /etc/mongo/mongod.conf"
    logging:
      driver: "json-file"
      options:
        max-size: "200m"
        max-file: "10"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
      - MONGO_INITDB_DATABASE=testdb
    ports:
      - 27022:27017
    volumes:
      - ./mongodb3:/data/db
      - ./mongocfg3:/etc/mongo
    container_name: mongo3
Bash
mkdir mongodb{1..3} && mkdir mongocfg{1..3}

Create some bs ssl key:

Bash
openssl rand -base64 666 > mongocfg1/mongo.key
Bash
sudo chmod 400 mongocfg1/mongo.key && sudo chown systemd-coredump mongocfg1/mongo.key
Bash
sudo cp -pr mongocfg1/mongo.key mongocfg2/mongo.key && \
cp -pr mongocfg1/mongo.key mongocfg3/mongo.key

nano mongocfg1/mongod.conf

Text Only
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
#  dbPath: /var/lib/mongodb
  journal:
    enabled: true
  engine: wiredTiger
#  mmapv1:
#  wiredTiger:

# where to write logging data.
#systemLog:
  #destination: syslog
  #logAppend: true
  #path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIpAll: true
#  bindIp: 127.0.0.1

# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
#  authorization: enabled
  keyFile: /etc/mongo/mongo.key

#operationProfiling:

#replication:
  #replSetName: durbok-replica

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Copy this config to other config folders

Bash
cp -pr mongocfg1/mongod.conf mongocfg2/mongod.conf && \
cp -pr mongocfg1/mongod.conf mongocfg3/mongod.conf

Enter mongo1 container

Bash
docker exec -it mongo1 bash

Fire-this-awesome-command

Text Only
mongo -u admin -p admin --eval "rs.initiate({
 _id: \"durbok-replica\",
 members: [
   {_id: 0, host: \"mongo1\"},
   {_id: 1, host: \"mongo2\"},
   {_id: 2, host: \"mongo3\"}
 ]
})"
Text Only
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4f1b6b50-f15d-489c-a590-cecb0e45ba93") }
MongoDB server version: 4.2.6
{ "ok" : 1 }

Check-with-this-command

Text Only
mongo -u admin -p admin --eval "rs.status()"
Text Only
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a90de91b-fd9e-483b-b268-6afb9736f398") }
MongoDB server version: 4.2.6
{
        "set" : "durbok-replica",
        "date" : ISODate("2022-10-10T14:24:47.725Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1665411871, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2022-10-10T14:24:31.300Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1665411871, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2022-10-10T14:24:31.300Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1665411871, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1665411871, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2022-10-10T14:24:31.300Z"),
                "lastDurableWallTime" : ISODate("2022-10-10T14:24:31.300Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1665411870, 3),
        "lastStableCheckpointTimestamp" : Timestamp(1665411870, 3),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2022-10-10T14:24:30.565Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1665411859, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2022-10-10T14:24:30.628Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2022-10-10T14:24:31.290Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 16075,
                        "optime" : {
                                "ts" : Timestamp(1665411871, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-10-10T14:24:31Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1665411870, 1),
                        "electionDate" : ISODate("2022-10-10T14:24:30Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "mongo2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 28,
                        "optime" : {
                                "ts" : Timestamp(1665411871, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1665411871, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-10-10T14:24:31Z"),
                        "optimeDurableDate" : ISODate("2022-10-10T14:24:31Z"),
                        "lastHeartbeat" : ISODate("2022-10-10T14:24:46.578Z"),
                        "lastHeartbeatRecv" : ISODate("2022-10-10T14:24:47.263Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "mongo1:27017",
                        "syncSourceHost" : "mongo1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "mongo3:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 28,
                        "optime" : {
                                "ts" : Timestamp(1665411871, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1665411871, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-10-10T14:24:31Z"),
                        "optimeDurableDate" : ISODate("2022-10-10T14:24:31Z"),
                        "lastHeartbeat" : ISODate("2022-10-10T14:24:46.621Z"),
                        "lastHeartbeatRecv" : ISODate("2022-10-10T14:24:47.263Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "mongo1:27017",
                        "syncSourceHost" : "mongo1:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1665411871, 1),
                "signature" : {
                        "hash" : BinData(0,"/Hof9NRK3tjmarLFn57+/bdC2tU="),
                        "keyId" : NumberLong("7152889516020203523")
                }
        },
        "operationTime" : Timestamp(1665411871, 1)
}

Exit docker container and login to databases:

Bash
mongo -u admin -p admin --authenticationDatabase admin mongodb://127.0.0.1:27020,127.0.0.1:27021,127.0.0.1:27022/readPreference=primaryPreferred

More about replica set - How to create replica set official docs

Back to top