Wednesday, March 21, 2018

Using DNSMASQ to Containers DNS Resolution

-->
-->
Using DNSMASQ to Containers DNS Resolution

All these experiments have been done on Centos:7, this will elaborate the Containers running on the Docker Host will have DNS resolution with DNSMASQ running on Docker Host

It’s a lightweight and straightforward solution for service discovery which doesn’t require running additional containers

First, we need to install dnsmasq on Docker Host
yum install dnsmasq.x86_64



Create a new Docker-related config file in /etc/dnsmasq.d:
vi /etc/dnsmasq.d/docker-dns
Paste the following lines:
addn-hosts=/docker-container-hosts
interface=docker0
#bind-interfaces


Restart the Dnsmasq service
service dnsmasq restart

Now let’s determine the host IP address for the docker0 interface:
ip addr show dev docker0 | awk -F'[ /]*' '/inet /{print $3}'
Most likely it will print 172.17.0.1.

For demonstration purposes, create two synthetic Docker containers specifying the determined IP address as DNS, and also host name and a container name:
docker run -itd  --dns=172.17.0.1 --hostname=businesswork1 --name=bw1  tibco:3.0.0
docker run -itd --dns=172.17.0.1 --hostname=businesswork2 --name=bw2 tibco:3.0.0

Docker PS 
# docker ps
CONTAINER ID        IMAGE                                                   COMMAND                  CREATED             STATUS              PORTS                              NAMES
e95e45ca99e1        tibco:3.0.0                                             "/opt/tmo/docker-entr"   3 seconds ago       Up 2 seconds                                           bw2
6b4a3026c0df        tibco:3.0.0                                             "/opt/tmo/docker-entr"   10 seconds ago      Up 9 seconds                                           bw1

Now we are going to create a simple shell script for updating additional host entries based on running containers:
vi ~/update-docker-dns.sh

#!/bin/bash

# Domain name for containers
CONTAINER_DOMAIN=containers.demo.com

# Path to the addn-hosts file
CONTAINER_HOSTS=/docker-container-hosts

echo "# Auto-generated by $0" > $CONTAINER_HOSTS
for CID in `docker ps -q`; do
    IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CID`
    NAME=`docker inspect --format '{{ .Config.Hostname }}' $CID`
    echo "$IP  $NAME.$CONTAINER_DOMAIN" >> $CONTAINER_HOSTS
done

# Ask dnsmasq to reload addn-hosts
pkill -x -HUP dnsmasq


Make the script executable:
chmod +x ~/update-docker-dns.sh

And run it 
~/update-docker-dns.sh

Let’s check the contents of the generated hosts file:

cat /docker-container-hosts
[root@sdnrhel00561 ~]# cat /docker-container-hosts 
# Auto-generated by ./update-docker-dns.sh
172.17.0.4  businesswork2.containers.demo.com
172.17.0.3  businesswork1.containers.demo.com

Check that BW2 container is accessible from BW1:
# docker exec -it bw1 /bin/bash
# ping businesswork1.containers.demo.com
PING businesswork1.containers.demo.com (172.17.0.3) 56(84) bytes of data.
64 bytes from businesswork1 (172.17.0.3): icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from businesswork1 (172.17.0.3): icmp_seq=2 ttl=64 time=0.043 ms
^C
--- businesswork1.containers.demo.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.034/0.038/0.043/0.007 ms

Just do not forget to provide correct --dns, --name and --hostname flags in the docker run command line! And Run the Script. 
The script will update the static DNS Container Hostnames when you run it.