Docker – Resolve DNS for a docker container with dnsmasq

dnsmasqdocker

I have a dnsmasq service that is currently bound to 127.0.0.1 pointing to my minikube instance like so:

address=/.k8s.local/192.168.39.184

I'd like to run docker containers that will communicate with the minikube instance using the DNS I specified.
When I set the container's DNS using docker run --dns 127.0.0.1 ... docker outputs the following message: WARNING: Localhost DNS setting (--dns=127.0.0.1) may fail in containers.
If I use the the host's network like so docker run --net host --dns 127.0.0.1 ... everything works as expected.
How do I configure the docker bridge to also resolve DNS using my local dnsmasq service?

Best Answer

You can use the host's local DNS resolver (e.g. dnsmasq) from your Docker containers if they are on a user defined network. In that case a container's /etc/resolv.conf will have the nameserver 127.0.0.11 (a.k.a. the Docker's embedded DNS server), which can forward DNS requests to the host's loopback address properly.

$ cat /etc/resolv.conf
nameserver 127.0.0.1
$ docker run --rm alpine cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
$ docker network create demo
557079c79ddf6be7d6def935fa0c1c3c8290a0db4649c4679b84f6363e3dd9a0
$ docker run --rm --net demo alpine cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0