配置 Docker Client,使 Docker 容器使用代理

https://docs.docker.com/network/proxy/

~/.docker/config.json 中:

{
 "proxies": {
   "default": {
     "httpProxy": "<http://proxy.example.com:3128>",
     "httpsProxy": "<https://proxy.example.com:3129>",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"
   },
   "tcp://docker-daemon1.example.com": {
     "noProxy": "*.internal.example.net"
   }
 }
}

在这之后,可以开一个容器打印环境变量来验证代理配置:

docker run --rm alpine sh -c 'env | grep -i  _PROXY'

配置 Docker Daemon,使 Docker 本体使用代理

https://docs.docker.com/config/daemon/systemd/

/etc/systemd/system/docker.service.d/http-proxy.conf 中:

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:3128"
Environment="HTTPS_PROXY=https://proxy.example.com:3129"
Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

同时注意 /etc/systemd/system/docker.service.d/ 下不能有 overrides.conf 之类的文件覆盖原有配置。

重启以应用配置:

sudo systemctl daemon-reload
sudo systemctl restart docker

在这之后,使用 docker info 就能看到已经被应用的代理配置:

Client: Docker Engine - Community
 Version:    26.0.2
 Context:    default
...
...
...
 HTTP Proxy: <http://192.168.0.108:7890>  <- 代理配置
 HTTPS Proxy: <https://192.168.0.108:7890>  <- 代理配置
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false