The Docker platform inherently supports the service discovery for the containers that are attached to any user-defined network using an embedded Domain Name Service (DNS). This functionality has been added to Docker since the version 1.10. The embedded DNS feature enables the Docker containers to discover each other using their names or aliases within the user-defined network. In other words, the name resolution request from the container is first sent to the embedded DNS. The user-defined network then uses a special 127.0.0.11 IP address for the embedded DNS, which is also listed in /etc/resolv.conf.
The following example will help to gain a better understanding of Docker's built-in service discovery capability:
- Let's begin by creating a user-defined bridge network, mybridge, using the following command:
$ sudo docker network create mybridge
- Inspect the newly created network to understand the subnet range and gateway IP:
$ sudo docker network inspect mybridge
[
{
"Name": "mybridge",
"Id": "36e5e088543895f6d335eb92299ee8e118cd0610e0d023f7c42e6e603b935e17",
"Created":
"2017-02-12T14:56:48.553408611Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
Here, the subnet assigned to the mybridge network is 172.18.0.0/16 and the gateway is 172.18.0.1.
- Now, let's create a container by attaching it to the mybridge network, as shown here:
$ sudo docker container run
-itd --net mybridge --name testdns ubuntu
- Continue to list the IP address assigned to the container, as illustrated here:
$ sudo docker container inspect --format
'{{.NetworkSettings.Networks.mybridge.IPAddress}}'
testdns
172.18.0.2
Evidently, the testdns container is assigned a 172.18.0.2 IP address. The 172.18.0.2 IP address is from the subnet of the mybridge network (that is, 172.18.0.0/16).
- Having got the IP address of the container, let's look into the content of the /etc/resolv.conf file of the container using the docker container exec subcommand, as shown here:
$ sudo docker container exec testdns
cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
Here the nameserver is configured as 127.0.0.11, which is the IP address of the embedded DNS.
- As a final step, let's ping the testdns container using the busybox image. We picked the busybox image here because the ubuntu image is shipped without the ping command:
$ sudo docker container run --rm --net mybridge
busybox ping -c 2 testdns
PING testdns (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64
time=0.085 ms
64 bytes from 172.18.0.2: seq=1 ttl=64
time=0.133 ms
--- testdns ping statistics ---
2 packets transmitted, 2 packets received,
0% packet loss
round-trip min/avg/max = 0.085/0.109/0.133 ms
Awesome, isn't it! The folks behind Docker have made it so simple that with no effort we are able to discover the containers in the same network.