Docker Networking - DNS Enable
In this guide, we will see how we can ping from one container to another container with the container's hostname using a custom bridge network.
By default when the bridge network is created, the DNS is not enabled. But if we create our custom bridge network DNS is enabled by default.
Below is the output of two containers created in the default bridge network trying to ping each other -
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
gaurav@learning-ocean:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
6c51373f78ac bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
gaurav@learning-ocean:~$ docker container run -itd ubuntu:14.04 bash
7b1af2ee48e43f8018c4324bbcb9f52a27f741bd7a0437ddd0f6766bd7ca6b10
^[[Agaurav@learning-ocean:~$ docker container run -itd ubuntu:14.04 bash
c3ce5dbe5d859705f139e811bc11367d02bf0969492e9d515a3cc6cc636ddfbb
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3ce5dbe5d85 ubuntu:14.04 "bash" 8 seconds ago Up 7 seconds unruffled_sinoussi
7b1af2ee48e4 ubuntu:14.04 "bash" 10 seconds ago Up 9 seconds trusting_joliot
gaurav@learning-ocean:~$ docker container exec 7b bash
gaurav@learning-ocean:~$ docker container exec -it 7b bash
root@7b1af2ee48e4:/# ping c3ce5dbe5d85
ping: unknown host c3ce5dbe5d85
root@7b1af2ee48e4:/#
To achieve the above use case, let's create a network 'test' first-
gaurav@learning-ocean:~$ docker network create test
b1e05c1afdb2f901e81a66a52d64a9dcdca9c5cab98433cdaed2faa83c5b3e6b
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b07b61bb20 ubuntu "bash" 24 seconds ago Up 23 seconds clever_wu
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c830b5de5766 ubuntu "bash" 4 seconds ago Up 3 seconds loving_raman
c7b07b61bb20 ubuntu "bash" About a minute ago Up About a minute clever_wu
gaurav@learning-ocean:~$
Now, create a container with ubuntu image with network as 'test' -
gaurav@learning-ocean:~$ docker container run -itd --network=test ubuntu:14.04 bash
c7b07b61bb20cdbb6e1b54a165aed0f8907d95d563fdd7a60940d004694c4557
list containers
gaurav@learning-ocean:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b07b61bb20 ubuntu "bash" About a minute ago Up About a minute clever_wu
Again create a new container in within the same network 'test' and ping the container created before -
gaurav@learning-ocean:~$ docker container run -it --network=test ubuntu:14.04 bash
c830b5de5766bc1475f4e15ea5da0a7171e1e185ee8fe9e4f060e9246c9299a9
root@c830b5de5766:/# ping c7b07b61bb20
PING c7b07b61bb20 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.078 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.135 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.132 ms
^C--- c7b07b61bb20 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.078/0.115/0.135/0.026 ms
root@c830b5de5766:/#
The packets are properly transmitted and received with 0% packet loss.
How to enable DNS in default bridge n/w ? Delete the default bridge and create a new bridge with the same name as "bridge" and your DNS will be enabled and it will be treated as default bridge as well.