macOS + Colima 下 docker pull 超时问题排查总结
一、问题现象
在 macOS 上使用 colima 运行 Docker service 时,执行:
bash
docker pull BALABALA
报错:
text
Error response from daemon:
Get "https://registry-1.docker.io/v2/":
net/http: request canceled while waiting for connection
(Client.Timeout exceeded while awaiting headers)
二、核心结论(先给答案)
Docker daemon 无法直连 Docker Hub,必须通过代理访问,但 daemon 并没有正确使用代理配置
shell / curl 能走代理 ≠ Docker daemon 会走代理
必须通过 systemd 给 Docker daemon 显式配置代理。
三、完整排查思路
1️⃣ 确认是否是网络问题,而非镜像本身
bash
docker pull hello-world
- 同样超时 → 不是镜像名问题,而是 Docker 出网问题
2️⃣ 进入 Colima VM,直接测试网络
bash
colima ssh
DNS 测试(正常)
bash
getent hosts registry-1.docker.io
# 也可以 nslookup
→ 能解析到 IP,说明 DNS 正常
HTTPS 连通性测试(关键)
bash
curl -Iv https://registry-1.docker.io/v2/
输出显示:
text
Uses proxy env variable https_proxy == 'http://PROXY_INFO_HERE'
CONNECT registry-1.docker.io:443
→ curl 在 通过代理访问 Docker Hub
3️⃣ 判断:是否"必须走代理"
清空所有代理变量,强制直连:
bash
env -u https_proxy -u http_proxy -u all_proxy -u no_proxy \
-u HTTPS_PROXY -u HTTP_PROXY -u ALL_PROXY -u NO_PROXY \
curl -Iv https://registry-1.docker.io/v2/ --max-time 15
结果:
text
Connection timed out
✅ 结论明确:
直连 Docker Hub 会超时,必须通过代理访问
4️⃣ 问题根因定位
查看 VM 内环境变量:
bash
env | egrep -i "https?_proxy|all_proxy|no_proxy"
text
HTTPS_PROXY=http://HERE
https_proxy=http://ARE
http_proxy=http://MY
HTTP_PROXY=http://PROXY
➡️ Shell / curl 有代理
⚠️ 但 Docker daemon 并不会自动继承这些环境变量
四、最终解决方案(关键步骤)
✅ 通过 systemd 给 Docker daemon 固化代理配置
bash
colima ssh
sudo mkdir -p /etc/systemd/system/docker.service.d
创建代理配置:
bash
sudo tee /etc/systemd/system/docker.service.d/proxy.conf >/dev/null <<'EOF'
[Service]
Environment="HTTP_PROXY=http://PROXY"
Environment="HTTPS_PROXY=http://HERE"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF
让 systemd 重新加载并重启 Docker:
bash
sudo systemctl daemon-reload
sudo systemctl restart docker
exit
✅ 验证配置是否生效
bash
colima ssh -- systemctl show docker -p Environment
确认能看到:
text
Environment=HTTP_PROXY=... HTTPS_PROXY=...
✅ 最终验证
bash
docker pull sean908/THE_IMAGE_YOU_NEED
🎉 拉取成功,问题解决
五、关键经验总结(非常重要)
1️⃣ macOS 上的代理 ≠ Colima VM 的代理
2️⃣ Shell 的代理 ≠ Docker daemon 的代理
3️⃣ Docker daemon 必须用 systemd 显式配置代理
4️⃣ 排查顺序永远是:
DNS → 直连 → 代理 → daemon 是否真正使用代理
六、TL;DR
在 Colima / Lima / Linux VM 中,
docker pull超时,90% 是 Docker daemon 没正确走代理;curl 能通不代表 docker 能通。