-1

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.

  • Does this answer your question? [What is linux equivalent of "host.docker.internal"](https://stackoverflow.com/a/62431165/9208887) – The Fool Dec 07 '22 at 10:45
  • If I understand correctly, "host.docker.internal" is used to access my local machine from a docker container. I'm trying to do the opposite. – Matthieu Alcaro Dec 07 '22 at 12:30

2 Answers2

1

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
Ralle Mc Black
  • 1,065
  • 1
  • 8
  • 16
  • Hey Ralle, thanks for your answer. I edited my question so you can see the interesting part of my docker-compose.yaml – Matthieu Alcaro Dec 07 '22 at 13:26
  • Allthough this will start if you add volumes and networks in the Docker-compose.yaml, For replication you need more than 1 node. If you look to the mongoDb logs of your container, you will find messages like: Replication has not been yet configured. Here is a nice descriptions how you ca do that: https://blog.devgenius.io/how-to-deploy-a-mongodb-replicaset-using-docker-compose-a538100db471 – Ralle Mc Black Dec 07 '22 at 13:36
  • According to the output of `rs.status()`, the replication has been configured. Moreover, i can tell my mongo database is working since i can access it with all of my services. PS: I'm adding edits to my question to give more details as i go along – Matthieu Alcaro Dec 07 '22 at 13:54
  • docker logs . You will see the the message i mententioned above. A replica can only be achieve when you use multiple nodes (instances of mongodb). At the moment you are using the mongoDb server as a standalone configuration. – Ralle Mc Black Dec 07 '22 at 14:06
  • To access from Compass use localhost, you cant use the uri from the docker, as it is known only to the docker network. – Ralle Mc Black Dec 07 '22 at 14:11
  • There is no such a comment except the firsts few seconds at launch, then the replicaSet is initialized correctly. I know, i can't use the uri from the docker, i'm connecting from my local machine's mongo shell with `mongo "mongodb://localhost:27017/"` and it is working correctly. The same uri doesn't work with Compass – Matthieu Alcaro Dec 07 '22 at 14:21
0

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 ?