Docker 配置代理

docker pull

docker pull 不会读取 http_proxy 环境变量,因为拉取请求是发给守护进程的(不清楚守护进程是否是经典的 listen-fork 模式),环境变量早就在启动时读入,需要专门配置 docker 守护进程并让它重新加载配置。可以参考: https://stackoverflow.com/a/71036185

根据 docker 官方文档 的说法,Docker Engine version 23.0 之前是需要让 daemon 启动时通过环境变量读取代理的(可以通过修改 systemctl 的 service 文件完成),现在也能通过修改 /etc/docker/daemon.json 文件来修改代理。尽管如此,我在 Docker Engine 24.0.7 上面测试,修改代理之后也需要完全重启 docker 才能让代理生效。这说明代理配置是在启动时读入,不可以运行时重载的

如果条件允许, 使用镜像源 是比配置代理更好的选择,它支持在运行时重载

docker build

docker build 的时候应该要用 --build-arg 传代理,然后还得大写

docker build . --build-arg HTTP_PROXY=$http_proxy --build-arg HTTPS_PROXY=$http_proxy ...

https://docs.docker.com/network/proxy/#set-proxy-using-the-cli

HTTPS_PROXYHTTP_PROXY 环境变量都得设置。只设置前者是不够的,因为有些连接需要 ssl。

根据 https://stackoverflow.com/a/28093517/

Footnote regarding HTTP_PROXY vs. HTTPS_PROXY: for a long time, setting HTTP_PROXY alone has been good enough. But with version 20.10.8, Docker has moved on to Go 1.16, which changes the semantics of this variable:

https://go.dev/doc/go1.16#nethttppkgnethttp

The ProxyFromEnvironment function no longer returns the setting of the HTTP_PROXY environment variable for https:// URLs when HTTPS_PROXY is unset.