Custom nameserver in Docker container

AI-generated summary

Custom nameserver in Docker container

The discussion revolves around customizing the /etc/resolv.conf file in a Docker container to use a local nameserver. andybalholm wants to resolve hostnames to their LAN addresses instead of external IP addresses.

mpalmer explains that the container’s resolv.conf is copied from the host, and there’s no way to override this behavior without editing the file manually after the container is started.

andybalholm shares the host’s and container’s resolv.conf files, revealing that the container’s file includes Google’s nameservers, despite the host’s file only specifying the local nameserver.

After rebuilding the container and editing resolv.conf, andybalholm finds that the changes are reverted on restart, suggesting that something is modifying the file on startup.

Finally, andybalholm discovers the solution by referencing Docker’s documentation on DNS configuration. By adding the line docker_args: --dns=10.33.55.1 to the container’s YAML file, they are able to achieve the desired resolv.conf configuration.

I want my Discourse installation to use my local nameserver. (So that it resolves the hostnames of the other computers in the same data center to their LAN addresses instead of the external IP.) The /etc/resolv.conf file in the container does include that nameserver, but it puts Google’s nameservers first. Is there any way to customize the content of resolv.conf?

As far as I’m aware, the container’s resolv.conf is copied from the host. So, if the container host has Google’s nameservers, then that’s what the container will get. AFAIK, there’s no way for a container image, nor the command line, to override that behaviour. Of course, you can edit the container’s resolv.conf after it is started, but there’s nothing in the Discourse images to do that that I can find. Certainly, the discourse.org hosting, which uses the same images as are publically available, don’t override resolv.conf (it definitely gets copied in from the host).

Well, here is the host’s 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
nameserver 10.33.55.1

And here is the one from the container:

nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 10.33.55.1

More strangeness:

I rebuilt the Discourse container, and edited resolv.conf to be just nameserver 10.33.55.1, and I could immediately resolve hostnames correctly. But whenever I restart the container, the contents of resolv.conf changes to:

nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 10.33.55.1

So something is running when the container starts, and adding the Google nameservers to resolv.conf. Or does the container filesystem revert to its previous state whenever it is restarted?

I found the documentation on how Docker creates and maintains /etc/resolv.conf: Networking overview | Docker Docs

It doesn’t seem to be doing exactly what it says there, but it told me enough to fix the problem. I added the following line to my container’s YAML file:

docker_args: --dns=10.33.55.1

And now I get the resolv.conf file I need.