6

I have a docker container 'A' on a host 10.11.12.13 listening on port 0.0.0.0:8443->8443/tcp I have another container 'B' on same host how wants to talk to 'A' on 8443 via host. Thus when I run command curl https://10.11.12.13:8443 inside B it gives me error as 'No route to host' but I can ping 10.11.12.13 successfully. I am not sure what else I am missing?

Can someone help regarding the issue?

  • 1
    Your container has exactly what it says: no route toward host, your container has a different IP and you'll have to give proper parameter to docker run for it to access your host, I can't remember them from memory but that's what to search for – Tensibai Dec 22 '17 at 22:16
  • @Tensibai But how can I able to ping successfully to host from container ? – Gourav Singla Dec 25 '17 at 05:24
  • I've tried to reproduce, started container A with command 'nc -l 8443' to listen to that port and started container B with command 'nc HOST_IP 8443' and it worked, containers could talk via host. – oryades Dec 25 '17 at 10:33

1 Answers1

4

Docker actually provides DNS support for networking between containers. This means that if you define your database connection as:

http://redis:6379

This will resolve redis to the correct network address, given that you fulfill the following:

  • All containers are on the same network
  • The containers have been named

This can either be done manually by starting both instances via the cli or automated with a docker compose script, which is my recommendation. In the compose script, from version 3, you would define the both services (aka the containers):

version: "3"
services:
  web:
    build: ./web
    container_name: web
    networks:
      - backend
    command: bash -c "./start.sh"

  redis:
    image: redis:3.2
    container_name: redis
    networks:
      - backend

And at the end of the file, define the network both instances should be connected to:

networks:
  backend:
Moritz
  • 1,217
  • 2
  • 11
  • 24
  • Version 2 of Docker Compose introduced service configuration, not 3. Also v3 is made for Docker Swarm while v2 has more host-oriented features, both are being developed independently and v2 is recommended for usage outside of Swarm cluster. – chupasaurus Feb 05 '18 at 20:40
  • You are right that it was introduced in v2. However, I could not find a reference supporting that they are being developed independently. 3 seems to be a simplification of 2: https://docs.docker.com/compose/compose-file/compose-versioning/#version-2x-to-3x – Moritz Feb 05 '18 at 20:51
  • Look at Releases page, new features are still added for v2, while some features from v2 are now supported in v3 (i.e. volume names in 3.4 which were in 2.1+). – chupasaurus Feb 05 '18 at 22:46