ubuntu无法拉docker镜像问题处理
ubuntu24
几次尝试:
- 第一次报错信息如下:
yeshen in ~ λ sudo docker pull budtmo/docker-android:emulator_9.0 Error response from daemon: Get "https://registry-1.docker.io/v2/": read tcp 10.249.51.62:53240->98.85.153.80:443: read: connection reset by peer
参考这个问答 https://stackoverflow.com/a/60224720/8305531
dig registry-1.docker.io +short
sudo vim /etc/hosts
增加一行配置:3.94.224.37 registry-1.docker.io
- 报错信息变了,变成如下
yeshen in ~ λ docker pull budtmo/docker-android:emulator_9.0 Error response from daemon: Head "https://registry-1.docker.io/v2/budtmo/docker-android/manifests/emulator_9.0": Get "https://auth.docker.io/token?account=xxx&scope=repository%3Abudtmo%2Fdocker-android%3Apull&service=registry.docker.io": read tcp 10.249.51.62:40916->98.85.153.80:443: read: connection reset by peer
dig auth.docker.io +short
sudo vim /etc/hosts
增加一行配置:3.94.224.37 auth.docker.io
- ok了
yeshen in ~ λ docker pull budtmo/docker-android:emulator_9.0
emulator_9.0: Pulling from budtmo/docker-android
32f112e3802c: Pull complete
fd0320870767: Pull complete
925dd4312eba: Pull complete
a631c95f4102: Pull complete
4f4fb700ef54: Pull complete
9e03ab21c06c: Pull complete
cedb1a91467d: Pull complete
a7e5b4ee501c: Pull complete
5611da61254c: Pull complete
96c174eaf3cf: Pull complete
eda8c3879a49: Pull complete
a1d0aa39e0aa: Pull complete
e8b559d6233a: Pull complete
94e4b2ac8c29: Pull complete
83d204066bc6: Pull complete
45e889d79f06: Pull complete
8becf497a696: Pull complete
142471240580: Pull complete
6c36f43ca238: Pull complete
77e34cc9b342: Pull complete
6d2d0b59e62a: Pull complete
486f942bc895: Pull complete
104f2cab59f8: Pull complete
19a38a23cdc8: Pull complete
e9a752c6462b: Pull complete
6f259ed1c92a: Pull complete
Digest: sha256:9eab37ade5b13b59494043e7720216358515459fe7e11c9e4d75bc3f1707dc9b
Status: Downloaded newer image for budtmo/docker-android:emulator_9.0
docker.io/budtmo/docker-android:emulator_9.0
原因分析:
- DNS 污染/劫持:
- 当执行
docker pull
时,客户端会向 DNS 服务器查询registry-1.docker.io
的 IP 地址。 - 如果本地 DNS 服务器(或网络中的中间设备)返回了错误的 IP 地址(例如被污染的地址),Docker 客户端会尝试连接到一个无效的服务器。
-
目标服务器可能直接拒绝连接或重置(
connection reset by peer
),导致拉取失败。 -
修改 hosts 为何有效?
- 修改
hosts
文件是手动将域名registry-1.docker.io
绑定到正确的 IP 地址,绕过了 DNS 查询步骤。 - 这证明原始 DNS 解析结果有问题,而直接指定 IP 后连接成功,进一步确认是 DNS 问题。
解决方案:
1. 永久修复 DNS 问题
-
更换公共 DNS 服务(推荐): ```bash # 临时测试(Linux/Mac) sudo echo "nameserver 8.8.8.8" >> /etc/resolv.conf
# 永久生效(根据系统配置网络) # 例如 Ubuntu:修改 /etc/systemd/resolved.conf 或 netplan 配置
`` 推荐 DNS 服务器: -
8.8.8.8(Google DNS) -
1.1.1.1(Cloudflare DNS) -
208.67.222.222`(OpenDNS) -
刷新 DNS 缓存: ```bash # Linux sudo systemd-resolve --flush-caches
# MacOS sudo killall -HUP mDNSResponder
# Windows ipconfig /flushdns ```
2. 检查 hosts 文件
- 确保
hosts
中指定的 IP 是 Docker Hub 的最新有效 IP(IP 可能变化):bash # 获取 registry-1.docker.io 的最新 IP nslookup registry-1.docker.io 8.8.8.8
将输出中的正确 IP 写入/etc/hosts
(Linux/Mac)或C:\Windows\System32\drivers\etc\hosts
(Windows):54.192.130.39 registry-1.docker.io
3. 检查网络环境
- 关闭 VPN/代理:某些 VPN 或代理可能导致 TLS 连接被重置。
- 防火墙规则:确保允许出站 443 端口(HTTPS)。
- 企业网络限制:联系 IT 部门确认是否拦截了 Docker Hub。
4. 配置 Docker 使用 HTTP 代理
如果问题由中间网络设备引起,可通过代理访问: bash # 编辑 Docker 服务配置 sudo mkdir -p /etc/systemd/system/docker.service.d sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
添加: ini [Service] Environment="HTTP_PROXY=http://proxy-ip:port" Environment="HTTPS_PROXY=http://proxy-ip:port"
重启 Docker: bash sudo systemctl daemon-reload sudo systemctl restart docker
总结
- 根本原因:DNS 污染/劫持导致
registry-1.docker.io
解析到错误 IP,触发连接重置。 - 解决优先级:
更换 DNS → 检查 hosts → 关闭代理 → 配置 HTTP 代理
建议优先使用公共 DNS(如8.8.8.8
),这是最稳定的解决方案。