Docker Community Forums

Share and learn in the Docker community.

Unable to resolve a container hostname with correct IP from DNS

docker

(Ove Ranheim) #1

Hello,

I wonder what I do wrong since I’m not able to resolve my hostname thru my DNS? Please see my steps below.

From HOST the IP is correctly resolved ‘mysql.example.com’ with the correct address

ubuntu@docker:~$ host mysql.example.com 172.17.0.1
Using domain server:
Name: 172.17.0.1
Address: 172.17.0.1#53
Aliases:

mysql.example.com has address 172.17.0.10

My /etc/default/docker configuration (and yes: I did a service docker restart)

DOCKER_OPTS="–dns 172.17.0.1 --dns 8.8.8.8"

Now try to fire up a container pointing at the correct DNS with search domain set up to ‘example.com

$ ubuntu@docker:~$ docker run --dns=172.17.0.1 --dns-search=example.com -h mysql.example.com -it ubuntu:trusty

Inside the newly started container the hostname is correctly set up

root@mysql:/# hostname
mysql.example.com

Inside my container the ‘resolv.conf’ is correctly set up

root@mysql:/# cat /etc/resolv.conf
search example.com
nameserver 172.17.0.1

Still, the container has not been able to be assigned with the IP from configured DNS !?!?!

root@mysql:/# ip a
92: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever

Now, I installed DnsUtils inside the container

root@mysql:/# apt-get install dnsutils
Reading package lists… Done
[…cut…]
The following NEW packages will be installed:
bind9-host dnsutils geoip-database krb5-locales libbind9-90 libdns100
libgeoip1 libgssapi-krb5-2 libisc95 libisccc90 libisccfg90 libk5crypto3
libkeyutils1 libkrb5-3 libkrb5support0 liblwres90 libxml2 sgml-base xml-core
0 upgraded, 19 newly installed, 0 to remove and 0 not upgraded.
[…cut…]

And finally, made a simple host lookup; which resolves the correct address

root@mysql:/# host mysql.example.com
mysql.example.com has address 172.17.0.10

The question remains: how come the container is designated with a wrong host ip? It should be ‘172.17.0.2’, but the address resolved from my DNS ‘172.17.0.10’.

What is the correct way to manage DNS configuration with Docker?


(Ove Ranheim) #2

Thanks to BlackthornYugen on docker@irc.freenode.net I figured out a solution on this. My problem wasn’t the dns; it was related to static IP or DHCP assignment. As of Docker Engine v1.10.1+ there is an option for assigning a static ip on a user defined network.

$ docker network create --subnet=172.18.0.0/16 mynet123 $ docker run --net=mynet123 --dns=172.17.0.1 --dns-search=example.com -h mysql.example.com --ip $(host -i mysql.example.com | sed -e "s/.*\ //" ) -it ubuntu:trusty
By doing a reverse NS lookup for --ip option using a bash eval; it seems to be sorted after all :slightly_smiling:

The container’s /etc/hosts now reads:
172.18.0.10 mysql.example.com mysql