--add-host 到底写进了哪里?它和 DNS 谁优先?在容器里直接改 /etc/hosts 能保存吗?这篇逐项实测,包括同名多条记录和 ping 与 nslookup 的差别。
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00:: ip6-localnet ff00:: ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters <容器IP> <容器ID前12位> hostname 命令:<容器ID前12位>
/etc/hosts 固定有 6 行本地和 IPv6 条目,最后一行是容器自己的 IP 和主机名(默认是容器 ID 前 12 位)。
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00:: ip6-localnet ff00:: ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 10.9.9.9 db.internal 10.9.9.10 cache <容器IP> web01.corp.test web01 hostname = web01;hostname -f = web01.corp.test docker inspect HostConfig.ExtraHosts = ["db.internal:10.9.9.9","cache:10.9.9.10"]
--add-host 名字:IP 追加在固定行之后;--hostname 与 --domainname 改的是最后一行,以及 hostname -f 的结果。
ping db.internal(只在 /etc/hosts 里):PING db.internal (10.9.9.9): 56 data bytes --add-host github.com:10.9.9.11 之后,ping github.com:PING github.com (10.9.9.11): 56 data bytes 同一个容器 nslookup github.com(busybox nslookup 直接问 DNS):Address: 20.205.243.166 对比:没加 --add-host 的容器 ping github.com:PING github.com (20.205.243.166): 56 data bytes
解析器(ping 用的 getaddrinfo)先查 /etc/hosts,所以可以用 --add-host 覆盖一个真实域名;但 busybox 的 nslookup 直接问 DNS,看不到 /etc/hosts 里的条目,排查解析问题时别被它误导。
--add-host multi:10.9.9.20 --add-host multi:10.9.9.21 → /etc/hosts 里 multi 的行:10.9.9.20 multi ; 10.9.9.21 multi ping multi 解析到:PING multi (10.9.9.20): 56 data bytes 容器里追加 10.9.9.99 edited.local 后,docker exec cat 能看到吗?能 docker restart 之后 edited.local 还在吗?不在;--add-host 的 multi 还在吗?在
同名多条,ping 取第一条。容器里手改的 /etc/hosts 在 docker restart 后丢失,而 --add-host 的条目会重新写入,所以要持久地加条目用 --add-host。