惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
S
Schneier on Security
A
Arctic Wolf
L
LangChain Blog
T
Threatpost
GbyAI
GbyAI
V2EX - 技术
V2EX - 技术
Jina AI
Jina AI
U
Unit 42
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
博客园 - 【当耐特】
S
Secure Thoughts
美团技术团队
N
News | PayPal Newsroom
爱范儿
爱范儿
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
S
Security @ Cisco Blogs
V
V2EX
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
腾讯CDC
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Security Archives - TechRepublic
Security Archives - TechRepublic
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest

Alliot's blog

Docker 代理配置机制与作用域 2025年底的安卓搞机备忘录 2025年底的安卓搞机备忘录 ArgoCD部署应用出现metadata.annotations过大问题 ArgoCD部署应用出现metadata.annotations过大问题 APC UPS更换电池校准容量 APC UPS更换电池校准容量 M1 Mac安装低版本Node.js M1 Mac安装低版本Node.js Ansible使用Bitwarden存储Vault密码 Ansible使用Bitwarden存储Vault密码 Cloudflare Tunnel前置代理支持 Cloudflare Tunnel前置代理支持 CDN场景下配置Vaultwarden启用fail2ban CDN场景下配置Vaultwarden启用fail2ban 中银香港丝滑开户总结 中银香港丝滑开户总结 从指定路径更新雷池WAF证书 从指定路径更新雷池WAF证书 AWS ECS使用EBS作为Volume AWS ECS使用EBS作为Volume 浅浅的调教一下国产智障电视 浅浅的调教一下国产智障电视 Nginx proxy_pass到AWS ALB的504问题 Nginx proxy_pass到AWS ALB的504问题 OpenV**手动指定路由规则 OpenV**手动指定路由规则 本地模拟CNAME解析 本地模拟CNAME解析 Nginx搭建WebDAV服务 Nginx搭建WebDAV服务 迎来船新版本的Hexo+NexT 迎来船新版本的Hexo+NexT 优雅的处理Git多帐号与代理问题 优雅的处理Git多帐号与代理问题 验光配镜扫盲 验光配镜扫盲 Prometheus relabel实现动态metrics path Prometheus relabel实现动态metrics path
Docker 代理配置机制与作用域
Alliot · 2026-02-08 · via Alliot's blog

  Docker 中的代理配置存在多个作用层级, 不同配置方式影响的范围完全不同。本文基于官方文档,对各层代理机制梳理说明, 以便区分。

Docker CLI 代理配置

配置文件: ~/.docker/config.json, 这个配置主要是针对 docker CLI 以及 docker compose 生效,配置示例如下:

1
2
3
4
5
6
7
8
9
{
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:1234",
"httpsProxy": "http://127.0.0.1:1234",
"noProxy": "localhost,127.0.0.1"
}
}
}

官方文档: https://docs.docker.com/engine/cli/proxy/

作用范围

对 build 的影响

在执行:

1
2
docker build
docker compose build

时,CLI 会将 proxy 自动转换为 build-args:

  • HTTP_PROXY
  • HTTPS_PROXY
  • NO_PROXY

官方说明:

Proxy settings are automatically passed as build arguments.

对应文档: https://docs.docker.com/engine/cli/proxy/#configure-the-docker-client

对容器运行的影响

当执行:

1
2
docker run
docker compose up

CLI 会自动将 proxy 注入为容器环境变量。

官方文档原文:

When you start a container, proxy environment variables are automatically set.

注意:该行为仅发生在使用当前 SHELL 用户执行 Docker CLI 创建容器时。


Dockerfile 中 ARG 与 ENV 的语义边界

官方文档:

ARG

1
ARG HTTP_PROXY
  • 仅在构建阶段可用
  • 不会自动出现在最终镜像的环境变量中
  • 必须通过 --build-arg 显式传入

官方说明:

ARG is not persisted in the final image.

ENV

1
2
ARG HTTP_PROXY
ENV HTTP_PROXY=$HTTP_PROXY
  • 会写入镜像元数据
  • 运行容器时自动存在
  • 可被 docker run -e 覆盖

官方说明:

The ENV instruction sets environment variables in the image.


Docker Daemon(dockerd)代理

systemd 配置方式, 创建如下文件

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

内容如下:

1
2
3
4
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:1234"
Environment="HTTPS_PROXY=http://127.0.0.1:1234"
Environment="NO_PROXY=localhost,127.0.0.1"

更新文件后, 使用 systemctl daemon-reload 重新加载, 并使用 systemctl restart docker 重启生效。

官方文档:https://docs.docker.com/config/daemon/systemd/#httphttps-proxy

作用范围

文档明确指出:

These environment variables configure the Docker daemon.

即:

仅影响 dockerd 自身网络行为,包括:

  • docker pull
  • docker login
  • registry 通信

不会影响

  • Dockerfile 构建阶段
  • 容器运行阶段环境变量

作用域对比

配置方式影响拉取镜像影响 build影响容器环境
~/.docker/config.json
Dockerfile ARG
Dockerfile ENV
dockerd systemd proxy

常见误区

误区 1:ARG 会自动进入运行时容器

错误。

ARG 仅存在于构建阶段, 除非显式转换为 ENV。

误区 2:config.json 仅影响 build

错误。
官方文档说明:

Proxy settings are used when starting containers.

因此同时影响 docker builddocker run命令。


推荐实践

构建阶段

显式传参:

1
2
3
docker build \
--build-arg HTTP_PROXY=... \
--build-arg HTTPS_PROXY=...

避免将代理写死在镜像中。

运行阶段

在 compose 文件中显式声明:

1
2
environment:
HTTP_PROXY: http://proxy:1234

不要依赖 CLI 自动注入。

Daemon 代理(最常用)

仅在宿主机需要代理访问 registry 时启用,比如在你拉镜像遇到网络问题的时候, 除了找镜像源, 你还可以走代理直接下载官方的 registry。