Can't connect from internal docker cluster to external mongodb service

If I run a single docker container of my backend, it works well and connects to mongodb which is running on the host. But when I run my backend with docker-compose it doesn't connect to mongodb and outputs to the console:

MongoError: failed to connect to server [12.345.678.912:27017] on first connect [MongoError: connection 0 to 12.345.678.912:27017 timed out]

      

Docker-compose.yml content:

version: "3"
services:
  web:
    image: __BE-IMAGE__
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      resources:
        limits:
          cpus: "0.1"
          memory: 2048M
    ports:
      - "1337:8080"
    networks:
      - webnet
  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "1340:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - webnet
networks:
  webnet:

      

how i start a separate docker container:

docker run -p 1337:8080 BE-IMAGE

      

+3


source to share


1 answer


you need to bind the mongo port as localhost is not the same as the inner and outer containers

ports:
  - "1337:8080"
  - "27017:27017"

      



In the port definitions, the left side is outside, the right side is the inside of your container ... Your error says the inside of your container is not seeing port 27017 ... the above just binds this mongo port so that the container can access that container port outside docker

0


source







All Articles