跳到正文

Docker 怎么设置代理:守护进程、Desktop、构建和容器

Docker 有四个互相独立的代理设置位置:拉取镜像的守护进程、Docker Desktop、构建过程,以及你运行的容器。本文逐个讲清楚,并说明怎么检查。

代理改变什么,不改变什么

代理只改变你指给它的那部分流量的网络出口,不改变镜像仓库或所拉镜像的条款,也不改变 Docker Desktop 自身的订阅规则:它的文档就写明,SOCKS5 支持需要 Business 订阅。Docker 文档还提醒,代理设置里可能含有敏感信息:环境变量可以通过远程 API 读到,也可能被提交进镜像,所以不要把带密码的代理写进你要分享的镜像里。

四个位置,四处设置

Docker 其实是好几个程序,每个都从自己的位置读取代理。守护进程(dockerd)是在后台下载镜像的服务,所以 docker pull 用的是守护进程的代理。Windows 和 Mac 上的 Docker Desktop 有自己的设置界面。构建在单独的构建容器里运行,代理值要通过构建参数传入。你运行的容器,只会从容器内部设置的变量里拿到代理。设置了其中一处,并不会设置其他几处,所以先判断是哪一处出了问题:拉取失败是守护进程或 Desktop,Dockerfile 的 RUN 行下载失败是构建,容器运行时里的程序连不上则是容器。

Linux 上拉取镜像:守护进程

在常规的 Linux 安装里,Docker 文档用的是 systemd drop-in:一个给 docker.service 追加环境变量的小文件。新建 /etc/systemd/system/docker.service.d/http-proxy.conf,写一个 [Service] 段,里面用 Environment 行设置 HTTP_PROXY、HTTPS_PROXY 和 NO_PROXY,然后运行 systemctl daemon-reload 和 systemctl restart docker。NO_PROXY 是用逗号分隔的、不走代理的地址列表,请保留 localhost 和 127.0.0.1。无根(rootless)模式下文件是 ~/.config/systemd/user/docker.service.d/http-proxy.conf,命令要加 --user。示例里最后一条命令会打印服务当前拥有的变量,这也是 Docker 页面建议的验证方法。

按同一页面所说,代理值里的特殊字符(如 #?!()[]{})必须用 %% 做双重转义。所以密码里的 # 在这个文件里要写成 %%23,而不是普通命令行里的 %23。HTTPS_PROXY 里同样写 http://:代理地址是同一个,只是经过它的流量是加密的。

用 systemd 设置守护进程:示例

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf >/dev/null <<'EOF'
[Service]
Environment="HTTP_PROXY=http://USERNAME:PASSWORD@HOST:PORT"
Environment="HTTPS_PROXY=http://USERNAME:PASSWORD@HOST:PORT"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl show --property=Environment docker

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

在 daemon.json 里做同样的事

也可以不用 systemd,而是在守护进程的配置文件里加 proxies 块:Linux 上是 /etc/docker/daemon.json,Windows 上是 C:\ProgramData\docker\config\daemon.json。键名是 http-proxy、https-proxy 和 no-proxy。改完后重启 Docker 守护进程。如果文件里已有别的设置,请把这个块加进现有的大括号里,不要另写一对。不要在 drop-in 和 daemon.json 里同时设置同一个代理,选一个即可。

在 daemon.json 里做同样的事:示例

{
  "proxies": {
    "http-proxy": "http://USERNAME:PASSWORD@HOST:PORT",
    "https-proxy": "http://USERNAME:PASSWORD@HOST:PORT",
    "no-proxy": "localhost,127.0.0.1"
  }
}

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

Docker Desktop(Windows 和 Mac)

  1. 打开 Settings > Resources > Proxies

    Docker Desktop 有两项代理设置。Docker Desktop proxy 管应用本身(登录、命令行、扩展)。Containers proxy 管 docker image pull,包括 Compose 的拉取。没设 Containers proxy 时,拉取会回退用 Desktop proxy。

  2. 选择 Manual configuration

    在 Web Server (HTTP) 和 Secure Web Server (HTTPS) 两个框里都填 http://HOST:PORT。排除列表里可以写不走代理的主机和域名。

  3. 按提示输入账号和密码

    对使用 Basic 认证的代理,文档说 Docker Desktop 会弹出提示让你输入凭据,并把它存进操作系统的凭据库。文档没有说能不能把账号密码直接写进网址输入框,所以请用弹出的提示。

  4. 应用并重启

    点击 Apply,等 Docker Desktop 重启,再用下面检查一节里的命令确认。

HTTP 还是 SOCKS5

Docker Desktop 支持 HTTP/HTTPS 和 SOCKS5 代理,但它的文档说 SOCKS5 需要 Business 订阅。Linux 守护进程那一页则完全没有提到 SOCKS。所以用于 Docker 的代理,生成时请选 HTTP,本文的示例也都是这样。

docker build 时的代理

Dockerfile 里的下载(比如 RUN apt-get update 或 RUN npm install)是在构建容器里运行的。Docker 为此预定义了构建参数:HTTP_PROXY、HTTPS_PROXY、FTP_PROXY、NO_PROXY 和 ALL_PROXY。用 --build-arg 传入即可,不必在 Dockerfile 里声明。它们默认不会进入构建缓存和 docker history,但如果你的 Dockerfile 里引用了它们,代理就会留在缓存里。如果你已经在 ~/.docker/config.json 里设了 proxies 块(见下一节),这些参数会自动帮你填好。不要用 ENV 指令写代理:Docker 页面建议改用构建参数,因为 ENV 会存进镜像。

docker build 时的代理:示例

docker build --build-arg HTTP_PROXY=http://USERNAME:PASSWORD@HOST:PORT \
  --build-arg HTTPS_PROXY=http://USERNAME:PASSWORD@HOST:PORT .

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

你运行的容器里的代理

Docker 客户端可以替你给每个新容器设置代理变量。在运行 docker 命令的那台机器上,给 ~/.docker/config.json 加一个 proxies 块。容器启动时,Docker 会在里面设置 HTTP_PROXY、http_proxy、HTTPS_PROXY、https_proxy、NO_PROXY 和 no_proxy。这个文件不用重启 Docker 就生效,但只对新容器和新构建有效,已经在运行的容器不受影响。只想给单个容器设置,可以改用 --env,见第二个示例。容器里的程序还得会读这些变量:curl 和很多工具会读,有些则不会。

同一页面还提醒:内部代理地址从容器里可能访问不到,代理值也能通过 API 读出来。请使用容器能连上的代理地址。

容器里的代理:~/.docker/config.json

{
  "proxies": {
    "default": {
      "httpProxy": "http://USERNAME:PASSWORD@HOST:PORT",
      "httpsProxy": "http://USERNAME:PASSWORD@HOST:PORT",
      "noProxy": "localhost,127.0.0.1"
    }
  }
}

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

只给一个容器设置代理:示例

docker run --rm --env HTTPS_PROXY=http://USERNAME:PASSWORD@HOST:PORT curlimages/curl -s https://ipinfo.io/json

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

怎么确认 Docker 在用代理

  1. 守护进程:看变量

    Linux 上运行 sudo systemctl show --property=Environment docker,找 HTTP_PROXY 和 HTTPS_PROXY。Docker Desktop 则看 Settings > Resources > Proxies。

  2. 拉取一个很小的镜像

    运行 docker pull hello-world。能下载,说明守护进程(或 Desktop)通过代理连上了镜像仓库。超时的话,用示例里的 curl 命令在 Docker 之外单独测试代理。

  3. 容器:打印变量

    docker run --rm alpine env | grep -i proxy 会显示新容器从 ~/.docker/config.json 得到的代理变量。

  4. 检查出口地址

    curl 命令会显示网站通过同一个代理看到的 IP、国家和网络。各字段的含义见「如何查看代理的 IP、国家和运营商」指南。

怎么确认 Docker 在用代理:示例

docker pull hello-world
docker run --rm alpine env | grep -i proxy
curl -x http://USERNAME:PASSWORD@HOST:PORT https://ipinfo.io/json

请把 HOST、PORT、USERNAME 和 PASSWORD 换成「我的资产」里的值。

常见错误

• 改了 drop-in 之后拉取仍超时。可能是没运行 systemctl daemon-reload 和重启,或文件名、[Service] 行写错了。用 systemctl show 打印变量看看。

• 407 Proxy Authentication Required。代理拒绝了账号或密码。systemd 和 daemon.json 文件里的特殊字符要按上面说的 %% 形式写;也可以从「我的资产」重新复制。

• 拉取成功,但 Dockerfile 的 RUN 行下载失败。构建不会用守护进程的代理:请传 --build-arg,或填写 ~/.docker/config.json。

• 正在运行的容器没有用上新的 config.json。它只对新容器有效,请启动一个新的。

• 一切正常,唯独本地服务连不上。把它们加进 NO_PROXY(localhost、127.0.0.1 和你的内部域名)。

用 Proxy302 怎么做

这里的每条命令都只需要 Proxy302 代理的主机、端口、账号和密码。动态住宅流量按 GB 计费,出口国家在生成代理时设置;HTTP 在 proxy.proxy302.com 的 2222 端口,SOCKS5 在 3333 端口。静态 ISP IP 在整个租期内固定、不轮换,适合希望每次都从同一个地址出去的工具。HTTP 还是 SOCKS5 在购买时选择,一个 IP 只带一种协议。

用 Proxy302 怎么做:步骤

  1. 第 1 步

    打开动态住宅页面,选择线路,「协议」保持 HTTP(或改选 SOCKS5),然后点击「确认费用并生成」。

  2. 第 2 步

    打开「我的资产」(控制台里放着你买到的全部代理),复制 HOST(地址)、PORT(端口)、USERNAME(账号)和 PASSWORD(密码)。「连接工具」页里有填好你自己连接信息的现成示例。

  3. 第 3 步

    把这些值填进上面的命令。

  4. 第 4 步

    通过代理访问 https://ipinfo.io/json 确认出口。

  5. 第 5 步

    在中国大陆,先在「连接工具」页打开辅助工具,使用代理期间保持开着。

价格与限制

低价线路每 GB 收费 US$1.50,100 GB 套餐每 GB US$1.35,300 GB 每 GB US$1.20。静态 ISP IP 一天 US$0.50,30 天 US$7.99。不提供免费试用,最小购买单位为 1 GB,或一个 IP 一天。可用的出口位置每天都会变化。

常见问题

Docker 能设 SOCKS5 代理吗?

Docker Desktop 写明支持 SOCKS5,但需要 Business 订阅;守护进程那一页没有提到 SOCKS。用于 Docker 的代理请选 HTTP。

为什么 docker pull 成功,Dockerfile 里的 npm install 却失败?

基础镜像是守护进程拉取的,而 RUN 行是在构建容器里运行的。请传 --build-arg HTTP_PROXY 和 HTTPS_PROXY,或使用 ~/.docker/config.json。

HTTPS_PROXY 必须以 https:// 开头吗?

不必。代理地址是同一个,通常以 http:// 开头,像示例里那样。代理本身是否使用 TLS,取决于它的配置。

守护进程的代理也适用于我的容器吗?

不适用。守护进程的代理只用于守护进程自己的下载,比如拉取镜像。容器的代理变量来自 ~/.docker/config.json 或 --env。

参考资料

6
  1. Docker docs: Daemon proxy configuration

    官方文档 · 确认日期:

    Read on 2 November 2026: the daemon.json proxies object with http-proxy, https-proxy and no-proxy (restart the daemon after editing); the systemd drop-in /etc/systemd/system/docker.service.d/http-proxy.conf with Environment lines for HTTP_PROXY, HTTPS_PROXY and NO_PROXY, then systemctl daemon-reload and systemctl restart docker; rootless mode uses ~/.config/systemd/user/docker.service.d/http-proxy.conf with systemctl --user; verification with systemctl show --property=Environment docker; NO_PROXY is a comma-separated list; special characters such as #?!()[]{} in the proxy value must be double escaped with %%.

    The page does not mention SOCKS proxies and does not use docker info for verification.

  2. Docker docs: Daemon configuration

    官方文档 · 确认日期:

    Read on 2 November 2026: default location of daemon.json is /etc/docker/daemon.json on Linux and C:\ProgramData\docker\config\daemon.json on Windows; a different file can be set with dockerd --config-file.

    The page read did not say how to restart the daemon.

  3. Docker docs: Configure the Docker client to use a proxy server

    官方文档 · 确认日期:

    Read on 2 November 2026: the proxies block in ~/.docker/config.json (httpProxy, httpsProxy, noProxy under "default"); it takes effect without restarting Docker but only for new containers and builds; containers get HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxy, NO_PROXY, no_proxy (and FTP_PROXY, ALL_PROXY for matching settings); docker run --env for one container; build arguments for proxies are pre-populated from the config file and are only available in the build container; do not use the ENV instruction for proxies; proxy settings may contain sensitive information and environment variables can be inspected through the remote API or committed to an image.

  4. Docker docs: Docker Desktop settings (Proxies)

    官方文档 · 确认日期:

    Read on 2 November 2026: Settings > Resources > Proxies; two settings, Docker Desktop proxy (host-level traffic, fallback for docker image pull) and Containers proxy (docker image pull, including Compose, and traffic of running containers when air-gapped container enforcement is configured); Manual configuration takes a Web Server (HTTP) and a Secure Web Server (HTTPS) URL in the form http://proxy:port or https://proxy:port and a list of hosts that skip the proxy; HTTP/HTTPS and SOCKS5 are supported, SOCKS5 requires a Business subscription; Basic authentication makes Docker Desktop ask for credentials and store them in the OS credential store, an https:// proxy URL protects the password in transit.

    The page section does not say how the proxy applies to docker build, and does not say whether user:password may be written inside the URL fields.

  5. Docker docs: Build variables (proxy arguments)

    官方文档 · 确认日期:

    Read on 2 November 2026: HTTP_PROXY, HTTPS_PROXY, FTP_PROXY, NO_PROXY and ALL_PROXY are pre-defined build arguments that need not be declared in the Dockerfile; example docker build --build-arg HTTP_PROXY=https://my-proxy.example.com .; they are excluded from the build cache and from docker history by default, unless the Dockerfile references them.

  6. Proxy302 product facts and price list

    产品观察 · 确认日期:

    Product facts file of the site (values checked against the console, the backend and the price table on 18-19 October 2026): products, prices in US$ (1 PTC = US$1), lease terms, protocols and ports, countries, quantity limits, no free trial.

准备好试试了吗?

注册账号、充值一次,就能选动态住宅、静态 ISP 或数据中心 IP,几分钟配进你的浏览器。