Home > other >  How to communicate between both docker containers and host machine
How to communicate between both docker containers and host machine

Time:12-08

I'm trying to deploy an API in a docker container, this API need to communicate with an other docker container (mongo) and some other services.

I start these containers using a docker-compose file, and both of the containers are in a user-defined bridge network. They can communicate between each others using the automatic DNS resolution, but in order to use Mongo Compass, I would like to access the mongo container from my local machine.

I've tried using the docker container's IP address : docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongo1 but i keep getting timeouts when i try to ping.

According to the documentation this behavior seems to be normal:

Using a user-defined network provides a scoped network in which only containers attached to that network are able to communicate.

But i think i am missing something, is there a way to achieve this ?

EDIT :

FYI here is an extract of my docker-compose.yaml file (as you can see, i am configuring mongo to have a replicaSet instance) :

  mongo1:
    container_name: mongo1
    image: mongo:5
    command: ["--replSet", "rs0", "--bind_ip_all"]
    volumes:
      - mongodb-volume:/data/db
    networks:
      - api-network
    ports:
      - 27017:27017
    restart: always
    healthcheck:
      test: test $$(echo "rs.initiate({_id:'rs0',members:[{_id:0,host:\"mongo1:27017\"}]}).ok || rs.status().ok" | mongo --port 27017 --quiet) -eq 1
      interval: 10s
      start_period: 30s

  api:
    container_name: api
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    command: npm run start:dev
    depends_on: 
      - mongo1
    ports:
      - 4000:4000
    networks:
      - api-network

And here is the logs from mongo when i try to connect using compass :

mongo1         | {"t":{"$date":"2022-12-07T13:27:57.556 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"192.168.176.1:58274","uuid":"48afaca9-6ad6-4f15-95fe-239935822907","connectionId":158,"connectionCount":12}}
mongo1         | {"t":{"$date":"2022-12-07T13:27:57.560 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn158","msg":"client metadata","attr":{"remote":"192.168.176.1:58274","client":"conn158","doc":{"driver":{"name":"nodejs","version":"4.10.0"},"os":{"type":"Darwin","name":"darwin","architecture":"x64","version":"21.5.0"},"platform":"Node.js v16.5.0, LE (unified)|Node.js v16.5.0, LE (unified)","application":{"name":"MongoDB Compass"}}}}
mongo1         | {"t":{"$date":"2022-12-07T13:27:57.567 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn158","msg":"Connection ended","attr":{"remote":"192.168.176.1:58274","uuid":"48afaca9-6ad6-4f15-95fe-239935822907","connectionId":158,"connectionCount":11}}

EDIT 2

It seems like i can access to my database using the mongo shell, but still can't access it using Compass even if the connection uri is the same.

CodePudding user response:

Add a port mapping in the docker-compose.yaml

Like

version: '3.7'
services:
  mongodb:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: mypassword
    ports:
      - 27018:27017
    volumes:
      - mongodb_data_container:/data/db

Now you can access MongoDb inside the docker network with mongodb:27017 and from your local machine on port 27018. You can substitute 27018 with a free port on your local machine and use localhost in the Compass configuration.

CodePudding user response:

After some research and testings, i can access my dockerized mongo instance from my local machine with Compass. But I have to set directConnection=true in the connection uri. Otherwise it doesn't work.

mongodb://localhost:27017/<my-db>?directConnection=true

==> WORKS

mongodb://localhost:27017/<my-db>?replicaSet=rs0&directConnection=true

==> WORKS but according to the doc, directConnection is not supported with replicaSet.

mongodb://localhost:27017/<my-db>

==> DOESN'T WORK

mongodb://localhost:27017/<my-db>?replicaSet=rs0

==> DOESN'T WORK

Can someone give me an explanation of what is going on ?

  • Related