docker-2
这个错误 Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers) 是 Docker 最常见的网络连接超时问题之一,本质上是你的机器无法及时连接到 Docker Hub 的 registry(registry-1.docker.io)。
它通常不是 Docker 本身坏了,而是网络/环境导致的请求在等待响应头时超时(默认超时 60 秒左右)。以下是按优先级排序的排查和修复步骤,从最常见到较少见,一步步试(建议从上往下执行,每次改完后重试 sudo docker pull hello-world)。
1. 最常见原因 & 快速修复(80% 的情况在这里解决)
-
网络不稳定 / 墙 / 运营商问题(尤其在中国大陆、某些企业网、VPN 环境)
→ 尝试换个网络:手机热点、其他 WiFi、关闭 VPN 再试。
→ 用手机热点测试通常能立刻验证是不是网络问题。 -
DNS 解析慢或被污染(非常高频)
临时改用公共 DNS(如 Google 或 Cloudflare):# 临时改(当前终端有效)
export http_proxy=""
export https_proxy=""
sudo systemd-resolve --set-dns=8.8.8.8 --set-dns=1.1.1.1 --interface=你的网卡名 # 如 eth0 或 enp0s3,用 ip link 看网卡名
# 或直接永久改 /etc/resolv.conf(重启后可能被覆盖,适合测试)
sudo nano /etc/resolv.conf
# 改成下面内容(或加在最上面)
nameserver 8.8.8.8
nameserver 1.1.1.1保存后立即重试 pull。
-
重启 Docker 服务(有时缓存或 daemon 卡住)
sudo systemctl restart docker
# 或
sudo service docker restart
2. 检查代理(如果你在公司/学校/需要代理的环境)
如果有 HTTP/HTTPS 代理,必须正确配置给 Docker daemon:
# 编辑或创建这个文件
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
写入(替换成你的代理地址):
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890/"
Environment="HTTPS_PROXY=http://127.0.0.1:7890/"
Environment="NO_PROXY=localhost,127.0.0.1"
然后重载并重启:
sudo systemctl daemon-reload
sudo systemctl restart docker
无代理时确保上面这些 Environment 行不存在或为空。
3. IPv6 问题(部分地区/设备常见)
如果你的网络 IPv6 不通,但 Docker 优先尝试 IPv6 地址:
-
临时禁用 IPv6(测试用):
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1 -
或加 hosts 强制走 IPv4(先查 registry-1.docker.io 的 IPv4):
nslookup registry-1.docker.io
# 挑一个 IPv4 如 52.55.191.55,添加到 /etc/hosts
sudo nano /etc/hosts
# 加一行:
52.55.191.55 registry-1.docker.io
4. 其他高频修复
-
检查防火墙 / SELinux(CentOS/RHEL 常见)
sudo firewall-cmd --add-service=http --add-service=https --permanent
sudo firewall-cmd --reload
# 或临时关(测试)
sudo systemctl stop firewalld -
Docker daemon 配置加 registry-mirror(加速 + 绕过部分问题,中国用户推荐)
编辑/etc/docker/daemon.json(没有就创建):{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
]
}然后:
sudo systemctl daemon-reload
sudo systemctl restart docker -
增加超时时间(临时 workaround)
sudo dockerd --max-concurrent-downloads 1 --debug & # 前台跑测试
# 或改 daemon.json 加 "max-concurrent-downloads": 1
5. 验证网络是否真的能连 registry
# 测试连通性
curl -v https://registry-1.docker.io/v2/
# 正常应该返回 401 Unauthorized(这是预期,不是错误)
# 如果 curl 也超时 → 纯网络问题,继续前面步骤
总结快速尝试顺序
- 换网络 / 关 VPN → 测试
- 改 DNS 为 8.8.8.8 + 1.1.1.1 → 测试
- 加 registry-mirror(中国用户必试)→ 重启 docker → 测试
- 配置正确代理(如果有)→ 测试
- 加 /etc/hosts 强制 IPv4 → 测试
如果以上都试了还是不行,请告诉我:
- 你的系统是什么?(Ubuntu? CentOS? Windows WSL? Mac? Raspberry Pi?)
- 你在哪个国家/地区?用的是什么网络(家宽/公司/云服务器)?
curl https://registry-1.docker.io/v2/的输出是什么?ping registry-1.docker.io是否通?
贴这些信息我能更精准帮你定位。