在服务器上使用 docker pull 镜像时一直报以下错误:
# docker search busybox
Using default tag: latest
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)
在 shell 中已经设置了 http 代理,测试 google 等外网也是可以访问的:
# export http_proxy=http://proxy.example.com:8080
# export https_proxy=http://proxy.example.com:8080
# curl -I www.google.com
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Cache-Control: private
Connection: keep-alive
此时使用 curl 测试 docker 镜像站点是否可访问:
{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}
从返回信息来看是 docker 账号未登陆,于是尝试使用 docker login 登陆账号,但在命令行执行 docker login 时失败:
bash
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: hyang0
Password:
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)
报错信息与之前相同,提示无法访问 registry 网站。使用环境变量控制代理在 docker 下没有生效,于是尝试在 .docker/config.json 配置文件中配置代理。
配置文件位置:
~/.docker/config.json
{
"proxies":
{
"default":
{
"httpProxy": "http://proxy.example.com:8080/",
"httpsProxy": "http://proxy.example.com:8080/",
"noProxy": "localhost,127.0.0.1,.example.com"
}
}
}
在实际执行 docker pull 时仍然无法连接docker站点。考虑到实际执行docker pull 时,docker 服务端会直接和 docker 站点通讯。所以准备对 docker daemon 也配置代理:
# mkdir -p /etc/systemd/system/docker.service.d
/etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
其中代理地址需要填写真实地址,这里填写的是 demo。
配置好服务端代理,需要重启docker daemon:
# systemctl daemon-reload
# systemctl restart docker
配置好 docker 服务端代理后,执行 docker pull 不再报错:
# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
3f4d90098f5b: Pull complete
Digest: sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
# docker search busybox
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
busybox Busybox base image. 3133 [OK]
rancher/busybox 0
openebs/busybox-client 1
antrea/busybox 1
总结,在对 docker 配置代理时,需要对 docker daemon 也配置代理。
参考
https://yeasy.gitbook.io/docker_practice/advanced_network/http_https_proxy
全文完。
如果转发本文,文末务必注明:“转自微信公众号:生有可恋”。
原文始发于微信公众号(生有可恋):执行 docker pull 时报错怎么办
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论