DNS not working within docker containers when host uses dnsmasq and Google's DNS server are firewalled?

后端 未结 6 1370
难免孤独
难免孤独 2020-12-24 03:09

The symptom is: the host machine has proper network access, but programs running within containers can\'t resolve DNS names (which may appear to be \"can\'t access the netwo

6条回答
  •  有刺的猬
    2020-12-24 03:50

    A clean solution is to configure docker+dnsmasq so than DNS requests from the docker container are forwarded to the dnsmasq daemon running on the host.

    For that, you need to configure dnsmasq to listen to the network interface used by docker, by adding a file /etc/NetworkManager/dnsmasq.d/docker-bridge.conf:

    $ cat /etc/NetworkManager/dnsmasq.d/docker-bridge.conf
    listen-address=172.17.0.1
    

    Then restart network manager to have the configuration file taken into account:

    sudo service network-manager restart
    

    Once this is done, you can add 172.17.0.1, i.e. the host's IP address from within docker, to the list of DNS servers. This can be done either using the command-line:

    $ sudo docker run -ti --dns 172.17.0.1 mmoy/ubuntu-netutils bash
    root@7805c7d153cc:/# ping www.example.com
    PING www.example.com (93.184.216.34) 56(84) bytes of data.
    64 bytes from 93.184.216.34: icmp_seq=1 ttl=54 time=86.6 ms
    

    ... or through docker's configuration file /etc/docker/daemon.json (create it if it doesn't exist):

    $ cat /etc/docker/daemon.json                      
    {
      "dns": [
        "172.17.0.1",
            "8.8.8.8",
            "8.8.4.4"
      ]
    }
    

    (this will fall back to Google's public DNS if dnsmasq fails)

    You need to restart docker to have the configuration file taken into account:

    sudo service docker restart
    

    Then you can use docker as usual:

    $ sudo docker run -ti mmoy/ubuntu-netutils bash
    root@344a983908cb:/# ping www.example.com
    PING www.example.com (93.184.216.34) 56(84) bytes of data.
    64 bytes from 93.184.216.34: icmp_seq=1 ttl=54 time=86.3 ms
    

提交回复
热议问题