Skip to content

Docker 代理配置

Docker daemon 的镜像拉取不受 shell 的 HTTP_PROXY 环境变量影响,需要单独在 systemd 级别注入代理。

配置 daemon 代理

bash
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf << 'EOF'
[Service]
Environment="HTTP_PROXY=http://<proxy-host>:<proxy-port>"
Environment="HTTPS_PROXY=http://<proxy-host>:<proxy-port>"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF

重启生效:

bash
sudo systemctl daemon-reload
sudo systemctl restart docker

验证

bash
docker run --rm hello-world

原理

  • docker pull / docker run 的镜像拉取由 Docker daemon 执行,不走 shell 代理
  • ~/.bashrc 里的 HTTP_PROXY 只对交互式 shell 启动的进程生效
  • Docker daemon 需要在其 systemd unit 中注入环境变量

如果环境内无代理,可改用 Docker 镜像加速器:在 /etc/docker/daemon.json 配置 registry-mirrors