Coder Social home page Coder Social logo

mongo_issue's Introduction

Trying and failing to setup MongoDB locally

The solution is to use the extra_hosts option in the docker-compose.yml file. See the extra_hosts section for more details.

This 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.

The setup is modeled after this link. I also read this issue.

The exact steps that should work but don't are:

$ docker compose up mongo --build
$ docker compose up app --build
$ ./check_status

The 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:

$ ./check_status
Current Mongosh Log ID: 65f83906c5927e356113556b
Connecting to:          mongodb://<credentials>@127.0.0.1:27020/foo?replicaSet=rs0&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.1.5
MongoNetworkError: getaddrinfo ENOTFOUND mongo

If 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.

Possible solutions

Modify the hosts file

The solution should be self-contained, and not require any manual setup on the host machine. This is why I'm not considering this solution.

network_mode: host

Doesn't work on MacOS. Applying the following Git diff won't work, likely due to this issue.

    diff --git a/docker-compose.yml b/docker-compose.yml
    index 9c268c0..7d5c8d6 100644
    --- a/docker-compose.yml
    +++ b/docker-compose.yml
    @@ -13,6 +13,7 @@ services:
         ]
     
       mongo:
    +    network_mode: "host"
         build:
           context: .
           args:
    @@ -20,11 +21,9 @@ services:
         environment:
           MONGO_INITDB_ROOT_USERNAME: root
           MONGO_INITDB_ROOT_PASSWORD: root
    -      MONGO_REPLICA_HOST: mongo
    +      MONGO_REPLICA_HOST: localhost
           MONGO_REPLICA_PORT: 27020
           MONGO_COMMAND: "mongosh"
    -    ports:
    -      - "27020:27020"
         restart: unless-stopped
         healthcheck:
           test: [ "CMD", "mongosh", "admin", "--port", "$$MONGO_REPLICA_PORT", "--eval", "db.adminCommand('ping').ok" ]

extra_hosts

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:

diff --git a/docker-compose.yml b/docker-compose.yml
index 9c268c0..1915d80 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,8 +4,10 @@ services:
   app:
     image: mongo:5
     entrypoint: mongosh 
+    extra_hosts:
+      - "localhost:host-gateway"
     command: [ 
-      "mongodb://root:root@mongo:27020/foo?replicaSet=rs0",
+      "mongodb://root:root@localhost:27020/foo?replicaSet=rs0",
       "--authenticationDatabase",
       "admin",
       "--eval",
@@ -20,7 +22,7 @@ services:
     environment:
       MONGO_INITDB_ROOT_USERNAME: root
       MONGO_INITDB_ROOT_PASSWORD: root
-      MONGO_REPLICA_HOST: mongo
+      MONGO_REPLICA_HOST: localhost
       MONGO_REPLICA_PORT: 27020
       MONGO_COMMAND: "mongosh"
     ports:

mongo_issue's People

Contributors

cidem avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.