Resolve DNS for a docker container with dnsmasq

10,368

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    
Share:
10,368

Related videos on Youtube

the_drow
Author by

the_drow

My name is Omer Katz and I am a very passionate software developer who specializes in ALM & SCM practices. I'm also an Agile & Lean enthusiast and practitioner. I am only 23, therefor I have only started working in the industry in 2010 but I have been programming since I was 11 and I have never stopped being passionate about it. I have a vast knowledge when it comes to planning, designing, developing & delivering software. My greatest passion is finding new ways to make the developers' lives easier and let them focus on what they know to do best, software development.

Updated on September 18, 2022

Comments

  • the_drow
    the_drow over 1 year

    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?