rosengrenen
rosengrenen

Reputation: 771

Docker DNS ignoring /etc/nsswitch.conf on host

I have an /etc/nsswitch.conf file that adds libvirt-nss, so that vm hostnames are automatically resolved on the host machine and in guest machines. I then have an nginx container (with --network host) running on the host, that routes traffic to some services on the host, and the rest to the virtual machines. The nginx container however cannot resolve the hostnames of the virtual machines, and I suspect the docker engine (?) does not respect /etc/nsswitch.conf of the host to resolve domain names. Is there any way to get this working or am I out of luck?

Upvotes: 2

Views: 1616

Answers (1)

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13616

As @DavidMaze pointed out, this is by design. There are several ways to achieve what you seem to be trying to do.

You can add an nsswitch.conf file to the container. This does work in Alpine. Either copy it in the Dockerfile or use a bind mount:

$ docker run --rm -v /etc/nsswitch.conf:/etc/nsswitch.conf --add-host foobar.dns:8.8.8.8 busybox ping foobar.dns
PING foobar.dns (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=113 time=7.796 ms
64 bytes from 8.8.8.8: seq=1 ttl=113 time=7.809 ms

You can then use docker --add-host to add hostnames to the container's hostfile. Alternatively, bind your host's hosts file as well:

$ docker run --rm -v /etc/nsswitch.conf:/etc/nsswitch.conf -v /etc/hosts:/etc/hosts busybox ping foobar.dns
PING foobar.dns (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=113 time=16.961 ms

Upvotes: 1

Related Questions