pmeerw's blog

06 Nov 2024

Wed, 06 Nov 2024

Docker DNS configuration woes

Background: DNS resolution on Linux is controlled by /etc/resolv.conf, where up to three nameservers can be configured among other things (search list, timeout, attempts, etc.)

Nameservers are queried in order, the second nameserver is only asked if there is no response from the first. When there is an answer (even a negative one), further nameservers are not consulted. This can be changed with the rotate option. The man page has more info.

How does Docker configure the container's DNS?

At least for bridged container network configuration (default), Docker mounts some host files into the container:

$ mount
...
/dev/nvme0n1p3 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro)
/dev/nvme0n1p3 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro)
/dev/nvme0n1p3 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro)

Hence, resolv.conf that the resolver service of the host uses is used by the container. I.e. on a system with systemd's resolved, the nameservers used by resolved will be used by the container, and NOT the local resolver 172.0.0.53. I don't know why, I think this makes no sense and complicates configuration.

The problem

All nameservers in /etc/resolv.conf shall return the same information. However, this is not the case if local, private and public nameservers are used. Private domains (such as example.local) can only be resolved by the private nameserver. This is in principle possible to configure in resolved, but not easily passed on to the container. In case multiple nameservers are configured for the container and the first local, private nameserver is unreliable or too slow, the fallback nameserver will be queried. This leads to sporadic host name lookup failure for private hosts on a local domain.

nameserver 172.x.y.z # private, can resolve example.local
nameserver 1.1.1.1 # public

Oh no, Snap!
Ubuntu can package docker as a Snap, adding some more complication...

The dockerd config file (--config-file=/var/snap/docker/nnnn/config/daemon.json) for Snap luckily lives in var/snap/docker/current/config/ and is editable, hurray!

Configuration changes

Edit /var/snap/docker/current/config/daemon.json to override DNS configuration for all containers:

{
    "dns": ["172.x.y.z"]
}
Restart the docker container service:
sudo snap restart docker

posted at: 19:05 | path: /configuration | permanent link

Made with PyBlosxom