DNS configuration

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", ...],
...
}
In all networking setups that I have ever worked on, I have not seen a situation where overriding DNS server IPs or DNS search prefixes is a better option to deploying your own DHCP server within the network and setting the appropriate options for the DNS server(s) (option 6) and domain name (option 15), which the machine will pick up when initializing the network interface. If you would like to find out more about these DHCP flags, I would highly recommend that you visit https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol#DHCP_options and read up on them before using the parameters we mentioned previously.
Caution! In some cases where the engine host's DNS servers are pointed to localhost ranges, as they are in most systemd-resolve and dnsmasq setups, the container cannot access the host's localhost address and is thus replaced with Google's DNS servers (8.8.8.8 and 8.8.4.4) by default for all containers running on that instance. If you would like to retain the host's DNS setting within the container, you must ensure that the DNS resolver in the configuration is not one on the localhost IP range and is accessible by container networks. You can find more information about this at https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset