{"id":19356247,"url":"https://github.com/mm-saiful6854/mongodb-replica-set-docker-compose","last_synced_at":"2026-01-25T01:31:50.263Z","repository":{"id":261542187,"uuid":"884601137","full_name":"mm-saiful6854/mongodb-replica-set-docker-compose","owner":"mm-saiful6854","description":"Docker compose file for MongoDB replica set. It guides you to setup replica set and test using MongoDB java driver.","archived":false,"fork":false,"pushed_at":"2024-11-07T09:55:51.000Z","size":9,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-23T10:45:53.706Z","etag":null,"topics":["docker-compose","mongo-java-driver","mongodb"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mm-saiful6854.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-07T03:37:21.000Z","updated_at":"2024-11-07T09:55:54.000Z","dependencies_parsed_at":"2024-11-07T05:28:43.491Z","dependency_job_id":"005987ab-8c02-473a-8ad6-713fcff4a9ec","html_url":"https://github.com/mm-saiful6854/mongodb-replica-set-docker-compose","commit_stats":null,"previous_names":["mm-saiful6854/mongodb-replica-set-docker-compose"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mm-saiful6854/mongodb-replica-set-docker-compose","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mm-saiful6854%2Fmongodb-replica-set-docker-compose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mm-saiful6854%2Fmongodb-replica-set-docker-compose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mm-saiful6854%2Fmongodb-replica-set-docker-compose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mm-saiful6854%2Fmongodb-replica-set-docker-compose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mm-saiful6854","download_url":"https://codeload.github.com/mm-saiful6854/mongodb-replica-set-docker-compose/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mm-saiful6854%2Fmongodb-replica-set-docker-compose/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28740900,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T01:25:41.653Z","status":"ssl_error","status_checked_at":"2026-01-25T01:25:34.364Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker-compose","mongo-java-driver","mongodb"],"created_at":"2024-11-10T07:03:44.658Z","updated_at":"2026-01-25T01:31:50.242Z","avatar_url":"https://github.com/mm-saiful6854.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongodb-replica-set-docker-compose\n\n## Steps of mongo replica set instantiation\n- clone this repository at your local host\n- apply docker compose command on file \".yaml\" file\n- interact with mongosh of any instance of mongo. Apply a command to initiate the replica set among them.\n- verify the replica-set status.\n\n\n\n### Apply docker compose command on file \".yaml\" file\n```\ndocker compose -f mongo-replica-set.yaml up -d\n```\n\"-f\" flag for indicating file. \"-d\" for run docker containers in detached mode.\n\n### Interact with mongosh of any instance of mongo cluster and apply a command to initiate the replica set among them\nLet's inspect the docker network to find out private ip addresses of mongodb instances.\n```\ndocker network inspect mongodb-replica-set-docker-compose_mongo-replica-cluster\n```\nOutput:\n```\n[\n    {\n        \"Name\": \"mongodb-replica-set-docker-compose_mongo-replica-cluster\",\n        \"Id\": \"9283da3d2bf7e8a55ff797874f4a16d4b2e0d4467efffe67f1226b43ac16ad99\",\n        \"Created\": \"2024-11-07T10:00:31.538627083+06:00\",\n        \"Scope\": \"local\",\n        \"Driver\": \"bridge\",\n        \"EnableIPv6\": false,\n        \"IPAM\": {\n            \"Driver\": \"default\",\n            \"Options\": null,\n            \"Config\": [\n                {\n                    \"Subnet\": \"172.22.0.0/16\",\n                    \"Gateway\": \"172.22.0.1\"\n                }\n            ]\n        },\n        \"Internal\": false,\n        \"Attachable\": false,\n        \"Ingress\": false,\n        \"ConfigFrom\": {\n            \"Network\": \"\"\n        },\n        \"ConfigOnly\": false,\n        \"Containers\": {\n            \"4bb2b660310210cb24c219ece06931e6a94e3be1cce302d57661e2380964430a\": {\n                \"Name\": \"mongo-replica-03\",\n                \"EndpointID\": \"7890f146ae0ecb40d2e9364d14c94780046e392270c27c49fd80200c281cba7c\",\n                \"MacAddress\": \"02:42:ac:16:00:02\",\n                \"IPv4Address\": \"172.22.0.2/16\",\n                \"IPv6Address\": \"\"\n            },\n            \"55a05fb5b15703e4ddd9611099b2ef904d7a3340071eb92ecb7ac1e7d81ff8b2\": {\n                \"Name\": \"mongo-replica-01\",\n                \"EndpointID\": \"3d59d420039eaab3e14531ff9e05c41ca6ea76c2c86b5d0f81538f0d2f9fa570\",\n                \"MacAddress\": \"02:42:ac:16:00:04\",\n                \"IPv4Address\": \"172.22.0.4/16\",\n                \"IPv6Address\": \"\"\n            },\n            \"8620e16269d899c1142a493140a45da5aac2ac23bf858e1890e9352be9b8c029\": {\n                \"Name\": \"mongo-replica-02\",\n                \"EndpointID\": \"cfa36697631e28b6e27a3f40ed7ddebd51659948c903bf930fd6f4cfc49bdba2\",\n                \"MacAddress\": \"02:42:ac:16:00:03\",\n                \"IPv4Address\": \"172.22.0.3/16\",\n                \"IPv6Address\": \"\"\n            },\n            \"99dd7d32e1cb8bb9e442032c8b54ff7128cc9027f96a22ad98bc21f35b776ed3\": {\n                \"Name\": \"mongo-express-replica-set\",\n                \"EndpointID\": \"c4e32510fbfdead8279f79ee5690b4a3386ac62c0ab20155e3d29975dd7d93ef\",\n                \"MacAddress\": \"02:42:ac:16:00:05\",\n                \"IPv4Address\": \"172.22.0.5/16\",\n                \"IPv6Address\": \"\"\n            }\n        },\n        \"Options\": {},\n        \"Labels\": {\n            \"com.docker.compose.network\": \"mongo-replica-cluster\",\n            \"com.docker.compose.project\": \"mongodb-replica-set-docker-compose\",\n            \"com.docker.compose.version\": \"2.29.7\"\n        }\n    }\n]\n```\nWe can find out the ip addresses such as '172.22.0.4', '172.22.0.3' and '172.22.0.2' for instances 1, 2, 3 respectively.\n\n\n```\ndocker exec -it mongoreplica01 mongosh\n```\nAfter getting into mongosh interactive shell, you check out the replica-set status.\n\n```\n\u003e rs.status()\n```\nOutput will be like this:\n```\nMongoServerError[NotYetInitialized]: no replset config has been received\n```\nIt means that replica-set is yet to configure.  To configure the replica-set, write this below command to instantiate:\n\n```\nrs.initiate({_id: \"mongoReplicaSet\", members:[{_id:0, host:\"172.22.0.4\"},{_id:1, host:\"172.22.0.3\"},{_id:2, host:\"172.22.0.2\"}]})\n```\n\nOutput:\n```\ntest\u003e rs.initiate({_id: \"mongoReplicaSet\", members:[{_id:0, host:\"172.22.0.4\"},{_id:1, host:\"172.22.0.3\"},{_id:2, host:\"172.22.0.2\"}]})\n{\n  ok: 1,\n  '$clusterTime': {\n    clusterTime: Timestamp({ t: 1730971950, i: 1 }),\n    signature: {\n      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),\n      keyId: Long('0')\n    }\n  },\n  operationTime: Timestamp({ t: 1730971950, i: 1 })\n}\nCongratulations!! Mongodb replica set is setup successfully.\n\n```\n### Verify the replica-set status.\n\nNow checking the status of cluster, you will get proper output:\n```\nmongoReplicaSet [direct: primary] test\u003e rs.status()\n{\n  set: 'mongoReplicaSet',\n  date: ISODate('2024-11-07T09:32:51.650Z'),\n  myState: 1,\n  term: Long('1'),\n  syncSourceHost: '',\n  syncSourceId: -1,\n  heartbeatIntervalMillis: Long('2000'),\n  majorityVoteCount: 2,\n  writeMajorityCount: 2,\n  votingMembersCount: 3,\n  writableVotingMembersCount: 3,\n  optimes: {\n    lastCommittedOpTime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n    lastCommittedWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n    readConcernMajorityOpTime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n    appliedOpTime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n    durableOpTime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n    writtenOpTime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n    lastAppliedWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n    lastDurableWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n    lastWrittenWallTime: ISODate('2024-11-07T09:32:41.787Z')\n  },\n  lastStableRecoveryTimestamp: Timestamp({ t: 1730971950, i: 1 }),\n  electionCandidateMetrics: {\n    lastElectionReason: 'electionTimeout',\n    lastElectionDate: ISODate('2024-11-07T09:32:41.694Z'),\n    electionTerm: Long('1'),\n    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1730971950, i: 1 }), t: Long('-1') },\n    lastSeenWrittenOpTimeAtElection: { ts: Timestamp({ t: 1730971950, i: 1 }), t: Long('-1') },\n    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1730971950, i: 1 }), t: Long('-1') },\n    numVotesNeeded: 2,\n    priorityAtElection: 1,\n    electionTimeoutMillis: Long('10000'),\n    numCatchUpOps: Long('0'),\n    newTermStartDate: ISODate('2024-11-07T09:32:41.737Z'),\n    wMajorityWriteAvailabilityDate: ISODate('2024-11-07T09:32:42.230Z')\n  },\n  members: [\n    {\n      _id: 0,\n      name: '172.22.0.4:27017',\n      health: 1,\n      state: 1,\n      stateStr: 'PRIMARY',\n      uptime: 1581,\n      optime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeDate: ISODate('2024-11-07T09:32:41.000Z'),\n      optimeWritten: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeWrittenDate: ISODate('2024-11-07T09:32:41.000Z'),\n      lastAppliedWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastDurableWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastWrittenWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      syncSourceHost: '',\n      syncSourceId: -1,\n      infoMessage: 'Could not find member to sync from',\n      electionTime: Timestamp({ t: 1730971961, i: 1 }),\n      electionDate: ISODate('2024-11-07T09:32:41.000Z'),\n      configVersion: 1,\n      configTerm: 1,\n      self: true,\n      lastHeartbeatMessage: ''\n    },\n    {\n      _id: 1,\n      name: '172.22.0.3:27017',\n      health: 1,\n      state: 2,\n      stateStr: 'SECONDARY',\n      uptime: 21,\n      optime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeDurable: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeWritten: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeDate: ISODate('2024-11-07T09:32:41.000Z'),\n      optimeDurableDate: ISODate('2024-11-07T09:32:41.000Z'),\n      optimeWrittenDate: ISODate('2024-11-07T09:32:41.000Z'),\n      lastAppliedWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastDurableWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastWrittenWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastHeartbeat: ISODate('2024-11-07T09:32:49.720Z'),\n      lastHeartbeatRecv: ISODate('2024-11-07T09:32:50.725Z'),\n      pingMs: Long('0'),\n      lastHeartbeatMessage: '',\n      syncSourceHost: '172.22.0.4:27017',\n      syncSourceId: 0,\n      infoMessage: '',\n      configVersion: 1,\n      configTerm: 1\n    },\n    {\n      _id: 2,\n      name: '172.22.0.2:27017',\n      health: 1,\n      state: 2,\n      stateStr: 'SECONDARY',\n      uptime: 21,\n      optime: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeDurable: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeWritten: { ts: Timestamp({ t: 1730971961, i: 16 }), t: Long('1') },\n      optimeDate: ISODate('2024-11-07T09:32:41.000Z'),\n      optimeDurableDate: ISODate('2024-11-07T09:32:41.000Z'),\n      optimeWrittenDate: ISODate('2024-11-07T09:32:41.000Z'),\n      lastAppliedWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastDurableWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastWrittenWallTime: ISODate('2024-11-07T09:32:41.787Z'),\n      lastHeartbeat: ISODate('2024-11-07T09:32:49.721Z'),\n      lastHeartbeatRecv: ISODate('2024-11-07T09:32:50.722Z'),\n      pingMs: Long('0'),\n      lastHeartbeatMessage: '',\n      syncSourceHost: '172.22.0.4:27017',\n      syncSourceId: 0,\n      infoMessage: '',\n      configVersion: 1,\n      configTerm: 1\n    }\n  ],\n  ok: 1,\n  '$clusterTime': {\n    clusterTime: Timestamp({ t: 1730971961, i: 16 }),\n    signature: {\n      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),\n      keyId: Long('0')\n    }\n  },\n  operationTime: Timestamp({ t: 1730971961, i: 16 })\n}\n```\n\n\n## Mongo-express for Mongodb UI\nWe sometimes feel comfortable with UI where we will get database information in interactive mode. Mongo express helps us to connect with mongodb. Let's setup mongo-express container.\n```\ndocker run -d --network mongodb-replica-set-docker-compose_mongo-replica-cluster -e ME_CONFIG_MONGODB_SERVER=mongoreplica01,mongoreplica02,mongoreplica03 --name mongo-express-replica-set -p 8081:8081 mongo-express\n```\nNow you might access 'http://localhost:8081/' to visualize the mongo express UI.\n\n\n\n## Reset the compose\nDocker compose provides commands to remove all container with volumes and networks.\n```\ndocker compose -f mongo-replica-set.yaml down -v\n```\nThe flag \"-v\" indicates docker to remove resources such as volumes and networks.\n\n\u003ci\u003eNote: host mapped volumes (e.g. ./volume/replica01) wouldn't be deleted though '-v' flag is provided in command. You have to delete manaually with root previleges.\u003c/i\u003e \n\n### How to delete volume manually in linux system\n```\n$ sudo su\n$ rm -rf volume/\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmm-saiful6854%2Fmongodb-replica-set-docker-compose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmm-saiful6854%2Fmongodb-replica-set-docker-compose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmm-saiful6854%2Fmongodb-replica-set-docker-compose/lists"}