我在 Ubuntu Server 22.04 上安装了 Docker(通过 apt 而不是 snap)。在我从 20.04 升级到 22.04 之前,一切都正常。现在,DNS 在 docker 中不起作用。
$ sudo docker run busybox nslookup google.com
Server: 1.1.1.1
Address: 1.1.1.1:53
Non-authoritative answer:
Name: google.com
Address: 172.217.0.78
*** Can't find google.com: No answer
如您所见,docker 正在尝试使用 1.1.1.1 来解析域名,但无法访问。如果我们在主机上执行同样的事情
$ nslookup google.com
Server: 1.1.1.1
Address: 1.1.1.1#53
Non-authoritative answer:
Name: google.com
Address: 172.217.0.78
Name: google.com
Address: 2607:f8b0:4025:815::200e
一切按预期进行。
Docker 肯定可以访问 DNS 服务器,因为我们可以 ping 它
$ sudo docker run busybox ping 1.1.1.1
PING 1.1.1.1 (1.1.1.1): 56 data bytes
64 bytes from 1.1.1.1: seq=0 ttl=56 time=13.370 ms
64 bytes from 1.1.1.1: seq=1 ttl=56 time=15.364 ms
64 bytes from 1.1.1.1: seq=2 ttl=56 time=11.075 ms
64 bytes from 1.1.1.1: seq=3 ttl=56 time=10.229 ms
64 bytes from 1.1.1.1: seq=4 ttl=56 time=11.113 ms
64 bytes from 1.1.1.1: seq=5 ttl=56 time=17.715 ms
^C
--- 1.1.1.1 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max = 10.229/13.144/17.715 ms
resolv.conf 非常典型
$ cat /etc/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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 1.1.1.1
nameserver 127.0.0.1
nameserver 172.17.0.1