元数据
| id | docker-container-dns | 收录 | 2026-08-29 |
|---|---|---|---|
| 问题域 | 编码代理与终端 | 主题分组 | 网络与镜像 |
| 适用工具 | docker · 外部工具 | ||
症状
容器内访问域名报解析失败(宿主上同样域名正常):
```
wget: bad address 'github.com' # busybox/alpine wget
curl: (6) Could not resolve host: github.com # curl
Temporary failure in name resolution # ping / apt-get 等 glibc 程序
```
特征:**IP 直连能通、域名不行**(`wget http://<IP>/` 成功证明是 DNS 层);宿主解析正常、容器内失败;常出现在给容器配过 `--dns`、或换了网络环境(校园网/公司网/代理)之后;WSL + Docker Desktop 升级后也可能偶发(for-win#13808)。
原因
容器用自己的 `/etc/resolv.conf` 做解析。默认 docker 会继承宿主的 nameserver 生成一份;但一旦用 `docker run --dns <ip>`、compose 的 `dns:`、或 daemon.json 的 `"dns"` 覆盖,容器就只问指定的 DNS。若该 DNS 不可达(国内网络配 `8.8.8.8` 是最经典坑:多数国内网络 UDP 53 被墙/丢包)、或域名本身只在特定 DNS 有记录(内网域名),解析就失败。错误文案取决于程序:busybox 报 `bad address`,glibc 程序报 `Temporary failure in name resolution`,同一问题不同长相。
解法
1. **先分层**:宿主 `getent hosts github.com` 正常 → 容器内 `cat /etc/resolv.conf` 看 nameserver 是什么(被覆盖过的会显示 `Overrides: [nameservers]`)。
2. **单容器指定可用 DNS(最常用)**:`docker run --dns 223.5.5.5 ...`(阿里)或 `--dns 119.29.29.29`(腾讯)——国内公共 DNS,实测可用。
3. **compose**:服务下加 `dns: [223.5.5.5, 119.29.29.29]`。
4. **全局默认**:daemon.json 加 `"dns": ["223.5.5.5", "119.29.29.29"]` 后重启 daemon(影响所有容器,改前确认)。
5. **配 8.8.8.8 前先测**(国内网络差异大):`timeout 5 bash -c 'echo > /dev/tcp/8.8.8.8/53'` 通了才用;不通就换国内公共 DNS。
6. **内网域名**:容器解析公司/校园内网域名失败时,把内网 DNS(或 VPN 分配的 DNS)加进 `--dns`,必要时与公共 DNS 并列。
验证证据
- 本地实测(2026-08-29,WSL Ubuntu,docker 29.1.3,alpine:3.20,校园网无代理):
- 基线:默认 DNS 继承宿主(resolv.conf 显示 nameserver 223.5.5.5 / 119.29.29.29),容器内 wget github.com 成功(exit 0);
- 复现:`--dns 192.0.2.1`(保留地址必然不可达)→ 容器内 `wget: bad address 'github.com'`(exit 1),resolv.conf 显示 `nameserver 192.0.2.1` + `Overrides: [nameservers]`;
- 经典坑实测:校园网 `8.8.8.8:53` 可达(`/dev/tcp` 探测通)→ `--dns 8.8.8.8` 解析成功(exit 0)——国内网络差异大,配前必须测;
- 修复:`--dns 223.5.5.5` → wget github.com 成功(exit 0)。
- 实测复现日志(2026-08-29)
来源与证据
素材来源:https://github.com/docker/for-win/issues/13808
验证证据:Evidence details kept locally (2026-08-29)