HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Networking between Docker containers

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
dockercontainersbetweennetworking

Problem

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?

Solution

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:

Code Snippets

http://redis:6379
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
networks:
  backend:

Context

StackExchange DevOps Q#2947, answer score: 4

Revisions (0)

No revisions yet.