{"id":14956793,"url":"https://github.com/upsync-dev/docker-compose-mongo-replica-set","last_synced_at":"2025-06-13T05:04:24.502Z","repository":{"id":42537331,"uuid":"331074422","full_name":"UpSync-Dev/docker-compose-mongo-replica-set","owner":"UpSync-Dev","description":"A way to run a Mongo replica set locally using Docker with minimal configuration required.","archived":false,"fork":false,"pushed_at":"2022-09-13T21:19:06.000Z","size":335,"stargazers_count":156,"open_issues_count":1,"forks_count":66,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-13T05:03:58.891Z","etag":null,"topics":["developer-tool","docker","docker-compose","docker-compose-template","mongo","mongodb","replica-set"],"latest_commit_sha":null,"homepage":"https://www.upsync.dev/2021/02/02/run-mongo-replica-set.html","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UpSync-Dev.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}},"created_at":"2021-01-19T18:37:06.000Z","updated_at":"2025-06-12T18:42:59.000Z","dependencies_parsed_at":"2023-01-18T06:45:53.022Z","dependency_job_id":null,"html_url":"https://github.com/UpSync-Dev/docker-compose-mongo-replica-set","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/UpSync-Dev/docker-compose-mongo-replica-set","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpSync-Dev%2Fdocker-compose-mongo-replica-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpSync-Dev%2Fdocker-compose-mongo-replica-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpSync-Dev%2Fdocker-compose-mongo-replica-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpSync-Dev%2Fdocker-compose-mongo-replica-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UpSync-Dev","download_url":"https://codeload.github.com/UpSync-Dev/docker-compose-mongo-replica-set/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UpSync-Dev%2Fdocker-compose-mongo-replica-set/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259584783,"owners_count":22880198,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["developer-tool","docker","docker-compose","docker-compose-template","mongo","mongodb","replica-set"],"created_at":"2024-09-24T13:13:32.366Z","updated_at":"2025-06-13T05:04:24.471Z","avatar_url":"https://github.com/UpSync-Dev.png","language":null,"readme":"# Running a Local Mongo Replica Set\n\n## Contents\n* [What is it?](#what-is-it)\n* [Versions](#versions)\n* [Are there any prerequisites?](#are-there-any-prerequisites)\n* [How do I run the Replica Set?](#how-do-i-run-the-replica-set)\n* [How do I access the Mongo Shells for each Instance?](#how-do-i-access-the-mongo-shells-for-each-instance)\n* [How does it work?](#how-does-it-work)\n* [Robo 3T](#robo-3t)\n* [Thanks / Further Reading](#thanks--further-reading)\n\n## Disclaimer\n\u003e :warning: **This setup is purely for local development purposes.**\n\u003e \n\u003e This setup should not be used for production applications as it was not built with that in mind. \n\n## What is it?\nThis `docker-compose` setup starts a local mongo replica set with 3 instances running on: \n- mongo1:30001\n- mongo2:30002\n- mongo3:30003\n\n## Versions\n* Different versions are available to cater for the various major versions of Mongo\n* You can find the most suitable one by looking at the [tags](https://github.com/UpSync-Dev/docker-compose-mongo-replica-set/tags) or [releases](https://github.com/UpSync-Dev/docker-compose-mongo-replica-set/releases) \n\n## Are there any prerequisites? \n* Docker\n* Docker Compose\n* The following in your `/etc/hosts` file:\n```\n127.0.0.1       mongo1\n127.0.0.1       mongo2\n127.0.0.1       mongo3\n```\n\n## How do I run the Replica Set?\nSimples:\n```\ndocker-compose up -d\n```\n\n## How do I access the Mongo Shells for each Instance?\n```\ndocker exec -it mongo1 sh -c \"mongo --port 30001\"\ndocker exec -it mongo2 sh -c \"mongo --port 30002\"\ndocker exec -it mongo3 sh -c \"mongo --port 30003\"\n```\n\n## How does it work?\n- Starts three instances of Mongo\n- On the first instance it runs the following Mongo Shell command:\n```\nrs.initiate(\n  {\n    _id : 'my-replica-set',\n    members: [\n      { _id : 0, host : \"mongo1:30001\" },\n      { _id : 1, host : \"mongo2:30002\" },\n      { _id : 2, host : \"mongo3:30003\" }\n    ]\n  }\n)\n```\n- This causes all 3 instances to join the replica set named `my-replica-set` and start talking to each other\n- One is elected to become the `PRIMARY` and the other two become `SECONDARY` instances\n- The Docker healthcheck config is used to cause the initialisation of the replica set. More info in the further reading links.\n\n## Robo 3T\nI used Robo 3T to test it locally and used the following config for the connection:\n\n![Robo 3T Config](https://github.com/UpSync-Dev/docker-compose-mongo-replica-set/raw/main/robo-3t.png)\n\n## Connecting with URI\n```\nmongodb://mongo1:30001,mongo2:30002,mongo3:30003/?replicaSet=my-replica-set\n```\n\n## Thanks / Further Reading\n- [How to turn standalone MongoDB server into a replica set with Docker-Compose](https://zgadzaj.com/development/docker/docker-compose/turning-standalone-mongodb-server-into-a-replica-set-with-docker-compose)\n- [Creating a MongoDB replica set using Docker 🍃](https://www.sohamkamani.com/blog/2016/06/30/docker-mongo-replica-set/)\n- [asoorm/docker-compose-mongo-replicaset.yml](https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fupsync-dev%2Fdocker-compose-mongo-replica-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fupsync-dev%2Fdocker-compose-mongo-replica-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fupsync-dev%2Fdocker-compose-mongo-replica-set/lists"}