Speaking of DNS, we should probably talk a bit about Docker DNS handling. By default, Docker Engine uses the DNS settings from the host, but in some advanced deployment settings where the network that the cluster is being deployed in is within an already built-out network, there may be times when the engine or the container needs to be configured with a custom DNS setting or the DNS search prefix (also know as the domain name). In such cases, you are able to override the default DNS settings of the Docker Engine easily by adding the dns and/or dns-search parameters to /etc/docker/daemon.json and restarting the daemon. Both parameters allow multiple values and are pretty self-explanatory:
{ ... "dns": ["1.2.3.4", "5.6.7.8", ...],
"dns-search": ["domain.com", ...],
...
}
If you are not interested in engine-wide configuration and are only trying to override a single container's DNS settings, you can do the equivalent action by adding --dns and --dns-search options to the docker run command, which ends up replacing the default /etc/resolv.conf settings in the relevant container:
$ # Since my default DNS is pointed to localhost, the default should be Google's DNS servers
$ docker run --rm
-it
ubuntu
/bin/cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 8.8.8.8
nameserver 8.8.4.4
$ # Now we will specify a custom DNS and DNS search prefix and see what the same file looks like
$ docker run --rm
-it
--dns 4.4.4.2
--dns-search "domain.com"
ubuntu
/bin/cat /etc/resolv.conf
search domain.com
nameserver 4.4.4.2
As you can see, the settings in the container have been changed to match our parameters. In our case, any DNS resolution will flow to the 4.4.4.2 server and any unqualified hostname will first be attempted to get resolved as <host>.domain.com.