Search code examples
dockerdocker-network

How does named container know each other's ip address in docker user created bridged network?


docker network create --driver bridge network1
docker run -itd --name docker1 --network network1 busybox
docker run -itd --name docker2 --network network1 busybox
docker exec -it docker1 /bin/bash

now in docker1, I can ping docker2

/ # ping docker2
PING docker2 (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.056 ms

I thought this entry is added in /etc/hosts, but it is not

/ # cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
172.19.0.2      841c0dc99115

How does docker1 know docker2's ip address?


Solution

  • Docker resolves names through internal dns server which is used by the Docker Daemon you can check the dns adress by cat /etc/resolv.conf to understand more you can read here