Yuhang Zheng

为docker pull命令增加代理

N 人看过

如果 docker 所在的环境是通过代理服务器和互联网连通的,那么需要一番配置才能让 docker 正常从外网正常拉取镜像。然而仅仅通过配置环境变量的方法是不够的。本文结合已有文档,介绍如何配置代理服务器能使docker正常拉取镜像。

本文使用的docker 版本是19.03

问题现象

如果不配置代理服务器就直接拉镜像,docker 会直接尝试连接镜像仓库,并且连接超时报错。如下所示:

$ docker pull 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)

容易误导的官方文档

有这么一篇关于 docker 配置代理服务器的 官方文档 ,如果病急乱投医,直接按照这篇文章配置,是不能成功拉取镜像的。

我们来理解一下这篇文档,文档关键的原文摘录如下:

If your container needs to use an HTTP, HTTPS, or FTP proxy server, you can configure it in different ways: Configure the Docker client On the Docker client, create or edit the file ~/.docker/config.json in the home directory of the user that starts containers.

When you create or start new containers, the environment variables are set automatically within the container.

这篇文档说:如果你的容器需要使用代理服务器,那么可以以如下方式配置: 在运行容器的用户 home 目录下,配置 ~/.docker/config.json 文件。重新启动容器后,这些环境变量将自动设置进容器,从而容器内的进程可以使用代理服务。

所以这篇文章是讲如何配置运行容器的环境,与如何拉取镜像无关。如果按照这篇文档的指导,如同南辕北辙。

要解决问题,我们首先来看一般情况下命令行如何使用代理。

环境变量

常规的命令行程序如果要使用代理,需要设置两个环境变量:HTTP_PROXYHTTPS_PROXY ,设置环境变量的方法见 这篇文章 。但是仅仅这样设置环境变量,也不能让 docker 成功拉取镜像。

我们仔细观察 上面的报错信息,有一句说明了报错的来源:

Error response from daemon:

因为镜像的拉取和管理都是 docker daemon 的职责,所以我们要让 docker daemon 知道代理服务器的存在。而 docker daemon 是由 systemd 管理的,所以我们要从 systemd 配置入手。

正确的官方文档

关于 systemd 配置代理服务器的 官方文档在这里,原文说:

The Docker daemon uses the HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environmental variables in its start-up environment to configure HTTP or HTTPS proxy behavior. You cannot configure these environment variables using the daemon.json file.

This example overrides the default docker.service file.

If you are behind an HTTP or HTTPS proxy server, for example in corporate settings, you need to add this configuration in the Docker systemd service file.

这段话的意思是,docker daemon 使用 HTTP_PROXY, HTTPS_PROXY, 和 NO_PROXY 三个环境变量配置代理服务器,但是你需要在 systemd 的文件里配置环境变量,而不能配置在 daemon.json 里。

具体操作

下面是来自 官方文档 的操作步骤和详细解释:

  1. 创建 dockerd 相关的 systemd 目录,这个目录下的配置将覆盖 dockerd 的默认配置
$ sudo mkdir -p /etc/systemd/system/docker.service.d
  1. 新建配置文件 /etc/systemd/system/docker.service.d/http-proxy.conf,这个文件中将包含环境变量
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"
  1. 如果你自己建了私有的镜像仓库,需要 dockerd 绕过代理服务器直连,那么配置 NO_PROXY 变量:
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80"
Environment="HTTPS_PROXY=https://proxy.example.com:443"
Environment="NO_PROXY=your-registry.com,10.10.10.10,*.example.com"

多个 NO_PROXY 变量的值用逗号分隔,而且可以使用通配符(*),极端情况下,如果 NO_PROXY=*,那么所有请求都将不通过代理服务器。

  1. 重新加载配置文件,重启 dockerd
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
  1. 检查确认环境变量已经正确配置:
$ sudo systemctl show --property=Environment docker
  1. docker info 的结果中查看配置项。

img

这样配置后,应该可以正常拉取 docker 镜像。

结论

docker 镜像由 docker daemon 管理,所以不能用修改 shell 环境变量的方法使用代理服务,而是从 systemd 角度设置环境变量。

参考资料

  1. https://stackoverflow.com/questions/69047394/cant-pull-docker-image-behind-a-proxy
  2. https://mikemylonakis.com/unix/docker-proxy/
  3. https://docs.docker.com/config/daemon/systemd/

补充:如何给APT命令设置代理 ?

使用代理文件

最简单方法是创建一个 proxy.conf 代理文件

$ sudo vi /etc/apt/apt.conf.d/proxy.conf

对于无用户名和密码的代理服务器,设置如下

(1)对于 HTTP Proxy, 添加如下条目

Acquire::http::Proxy "http://proxy-IP-address:proxyport/";

(2)对于 HTTPS Proxy,添加如下条目

Acquire::https::Proxy "http://proxy-IP-address:proxyport/";

示例如下

$ cat  /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://192.168.56.102:3128/";
Acquire::https::Proxy "http://192.168.56.102:3128/";

如果您的代理服务器需要用户名和密码,请按如下方式添加

Acquire::http::Proxy "http://username:password@proxy-IP-address:proxyport";
Acquire::https::Proxy "http://username:password@proxy-IP-address:proxyport";

示例如下

$ cat  /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";
Acquire::https::Proxy "http://init@PassW0rd321#@192.168.56.102:3128/";

完成后保存更改,代理设置将在下次运行 APT 包管理器时生效。

例如,您可以更新本地包索引,然后安装 net-tools 包

$ sudo apt update
$ sudo apt install net-tools -y

指定代理的另一种方法

除了第一种方法,您还可以用一种简单得多的方式指定代理。同样,按如下所示创建 proxy.conf 文件。

$ sudo vi /etc/apt/apt.conf.d/proxy.conf

对于没有用户名和密码的代理服务器,定义如下所示

Acquire {
  http::Proxy "http://proxy-IP-address:proxyport/";
  https::Proxy "http://proxy-IP-address:proxyport/";
}

示例如下

$ cat /etc/apt/apt.conf.d/proxy.conf
Acquire {
  http::Proxy "http://192.168.56.102:3128/";
  https::Proxy "http://192.168.56.102:3128/";
}

对于有用户名和密码的代理服务器,定义如下所示

Acquire {
   http::Proxy "http://username:password@proxy-IP-address:proxyport/";
   https::Proxy "http://username:password@proxy-IP-address:proxyport/";
}

保存更改,一旦您开始使用 APT 包管理器,这些设置就会立即生效。