{"id":16344245,"url":"https://github.com/cidem/mongo_issue","last_synced_at":"2026-02-23T02:02:25.580Z","repository":{"id":228751462,"uuid":"773812938","full_name":"cideM/mongo_issue","owner":"cideM","description":"Setting up a local MongoDB replica set is hard and this repository shows why","archived":false,"fork":false,"pushed_at":"2024-03-20T09:54:39.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-18T08:36:18.262Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cideM.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2024-03-18T12:53:39.000Z","updated_at":"2024-03-18T12:53:53.000Z","dependencies_parsed_at":"2024-03-20T09:54:56.557Z","dependency_job_id":"8877ca6a-14cc-4c0c-a828-f017cbdc8bca","html_url":"https://github.com/cideM/mongo_issue","commit_stats":null,"previous_names":["cidem/mongo_issue"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cideM/mongo_issue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cideM%2Fmongo_issue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cideM%2Fmongo_issue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cideM%2Fmongo_issue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cideM%2Fmongo_issue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cideM","download_url":"https://codeload.github.com/cideM/mongo_issue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cideM%2Fmongo_issue/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281047793,"owners_count":26435124,"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","status":"online","status_checked_at":"2025-10-25T02:00:06.499Z","response_time":81,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-10-11T00:27:35.435Z","updated_at":"2025-10-26T01:37:40.087Z","avatar_url":"https://github.com/cideM.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trying and failing to setup MongoDB locally\n\n**The solution is to use the `extra_hosts` option in the `docker-compose.yml` file.** See the [`extra_hosts`](#extra_hosts) section for more details.\n\nThis repository demonstrates the difficulty with setting up MongoDB locally using Docker Compose. The goal is to have a MongoDB replica set running as a Docker Compose service, and being able to connect to it from the host and from another Docker Compose service.\n\nThe setup is modeled after [this link](https://github.com/prisma/prisma/discussions/22442%C2%AC). I also read [this issue](https://github.com/prisma/docs/issues/3040).\n\nThe exact steps that should work but don't are:\n\n```shell\n$ docker compose up mongo --build\n$ docker compose up app --build\n$ ./check_status\n```\n\nThe first command sets up the database, the second and third just run the `rs.status()` command. Both should return successfully. Right now, the second command fails with this error:\n\n```text\n$ ./check_status\nCurrent Mongosh Log ID: 65f83906c5927e356113556b\nConnecting to:          mongodb://\u003ccredentials\u003e@127.0.0.1:27020/foo?replicaSet=rs0\u0026serverSelectionTimeoutMS=2000\u0026authSource=admin\u0026appName=mongosh+2.1.5\nMongoNetworkError: getaddrinfo ENOTFOUND mongo\n```\n\nIf you modify the Docker Compose setup, make sure to delete the volume that holds the MongoDB data with `docker compose down --volumes`. Otherwise changes might not be picked up.\n\n## Possible solutions\n\n### Modify the `hosts` file\n\nThe solution should be self-contained, and not require any manual setup on the host machine. This is why I'm not considering this solution.\n\n### `network_mode: host`\n\nDoesn't work on MacOS. Applying the following Git diff won't work, likely due to [this issue](https://github.com/docker/for-mac/issues/1031).\n\n```diff\n    diff --git a/docker-compose.yml b/docker-compose.yml\n    index 9c268c0..7d5c8d6 100644\n    --- a/docker-compose.yml\n    +++ b/docker-compose.yml\n    @@ -13,6 +13,7 @@ services:\n         ]\n     \n       mongo:\n    +    network_mode: \"host\"\n         build:\n           context: .\n           args:\n    @@ -20,11 +21,9 @@ services:\n         environment:\n           MONGO_INITDB_ROOT_USERNAME: root\n           MONGO_INITDB_ROOT_PASSWORD: root\n    -      MONGO_REPLICA_HOST: mongo\n    +      MONGO_REPLICA_HOST: localhost\n           MONGO_REPLICA_PORT: 27020\n           MONGO_COMMAND: \"mongosh\"\n    -    ports:\n    -      - \"27020:27020\"\n         restart: unless-stopped\n         healthcheck:\n           test: [ \"CMD\", \"mongosh\", \"admin\", \"--port\", \"$$MONGO_REPLICA_PORT\", \"--eval\", \"db.adminCommand('ping').ok\" ]\n```\n\n### `extra_hosts`\n\n**This seems to be the correct solution and it was tested on MacOS and Linux.** Run the MongoDB service in the Docker Compose network, but expose its port so we can connect to it from the host. Next, make other Docker Compose services connect to the host by mapping `localhost` to the `host-gateway`. You can't just use `host.docker.internal`, because the replica set is listening for connections on `localhost`. See the Git diff:\n\n```diff\ndiff --git a/docker-compose.yml b/docker-compose.yml\nindex 9c268c0..1915d80 100644\n--- a/docker-compose.yml\n+++ b/docker-compose.yml\n@@ -4,8 +4,10 @@ services:\n   app:\n     image: mongo:5\n     entrypoint: mongosh \n+    extra_hosts:\n+      - \"localhost:host-gateway\"\n     command: [ \n-      \"mongodb://root:root@mongo:27020/foo?replicaSet=rs0\",\n+      \"mongodb://root:root@localhost:27020/foo?replicaSet=rs0\",\n       \"--authenticationDatabase\",\n       \"admin\",\n       \"--eval\",\n@@ -20,7 +22,7 @@ services:\n     environment:\n       MONGO_INITDB_ROOT_USERNAME: root\n       MONGO_INITDB_ROOT_PASSWORD: root\n-      MONGO_REPLICA_HOST: mongo\n+      MONGO_REPLICA_HOST: localhost\n       MONGO_REPLICA_PORT: 27020\n       MONGO_COMMAND: \"mongosh\"\n     ports:\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcidem%2Fmongo_issue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcidem%2Fmongo_issue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcidem%2Fmongo_issue/lists"}