title: Cannot connect from inside docker swarm cluster to external mongodb service
body: <p>If I run single docker container of my backend, it runs well and connects to mongodb which is running on host. But when I run my backend using docker-compose, it doesn't connect to mongodb and prints to console:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>docker-compose.yml contents:</p>
  
  <pre><code>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:
  </code></pre>
  
  <p>how I run single docker container:</p>
  
  <pre><code>docker run -p 1337:8080 BE-IMAGE
  </code></pre>