Docker 和 containerd 代理配置教程
在公司内部网络、校园/科研网或带有防火墙/代理出口的环境中,Docker 从外部镜像仓库拉取镜像可能会被阻断或速度极慢。此时,就需要为 Docker 客户端、Daemon(守护进程)配置 HTTP/HTTPS 代理。在很多使用 containerd(如 Kubernetes 节点、云环境)中,如果节点需要走代理访问外网拉取镜像,也需要为 containerd 配置代理。虽然代理主要通过环境变量控制
一、Docker 代理配置教程
1. 背景说明
在公司内部网络、校园/科研网或带有防火墙/代理出口的环境中,Docker 从外部镜像仓库拉取镜像可能会被阻断或速度极慢。此时,就需要为 Docker 客户端、Daemon(守护进程)配置 HTTP/HTTPS 代理。官方文档对此已有说明。 ([Docker Documentation][1])
2. 配置流程
以下以 Linux(systemd 管理)为主,适用于大多数 Ubuntu、CentOS 等发行版。
2.1 配置 Docker 客户端(docker 命令行)
在 ~/.docker/config.json 中添加 proxy 配置:
{
"proxies": {
"default": {
"httpProxy": "http://proxy.example.com:3128",
"httpsProxy": "http://proxy.example.com:3129",
"noProxy": "localhost,127.0.0.1,.example.org"
}
}
}
这样,当你用 docker run 或 docker build 时,容器内部会继承这些环境变量。 ([Docker Documentation][1])
注意:这只对 “容器内” 的网络环境有效(即容器里要访问外部网络)。如果你只是拉镜像、Push 镜像,还需要配置 daemon。
2.2 配置 Docker 守护进程(daemon)
通过 systemd drop-in 文件设置环境变量
创建目录(如果不存在):
sudo mkdir -p /etc/systemd/system/docker.service.d
然后创建 /etc/systemd/system/docker.service.d/http-proxy.conf,内容如下:
[Service]
## 所谓代理也就是科学上网的工具 一般这个工具都有局域网链接功能 按照工具上的写就行
Environment="HTTP_PROXY=http://proxy.example.com:3128"
Environment="HTTPS_PROXY=http://proxy.example.com:3129"
Environment="NO_PROXY=localhost,127.0.0.1,10.0.0.0/8" ## 这个是不走代理的配置
然后执行:
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl show --property=Environment docker
以确认环境变量已生效。 ([Medium][3])
3. 测试验证
- 执行
docker info,查看是否显示了 http proxy / https proxy 设置。 - 执行
docker pull hello-world,看是否能成功拉取镜像。若失败,可看日志 / 检查代理服务器。 ([datacamp.com][4]) - 如果容器内部也需要访问外网,启动容器并执行
env | grep -i proxy,看是否正确继承。
4. 常见注意事项 & 问题排查
NO_PROXY/no_proxy要把内网地址(如 Kubernetes 集群内部、节点之间、registry 内部)都加进去,否则可能“代理后反而连不上”内网服务。 ([docs.k3s.io][5])- 如果使用的是 Docker Desktop(Windows/macOS),需要在 GUI 的 “Settings → Resources → Proxies” 中设置。 ([CloudBees][6])
- 代理带认证(用户名/密码)时,注意 URL 格式
http://user:pass@proxy:port。但因为明文存储,安全要考虑。 ([Docker Documentation][1]) - 如果
docker build时遇到无法通过代理拉取或 build 阶段失败,可在 Dockerfile 中使用ARG http_proxy=...、ARG https_proxy=...来传递。 ([DEV Community][7]) - 在一些较旧版本 Docker 中,
daemon.json不支持配置 http-proxy,此时必须用 systemd 环境变量方式。 ([GitHub][8])
二、containerd 代理配置教程
1. 背景说明
在很多使用 containerd(如 Kubernetes 节点、云环境)中,如果节点需要走代理访问外网拉取镜像,也需要为 containerd 配置代理。相对于 Docker,containerd 的文档不如 Docker 那么集中,但已有多篇实战经验总结。 ([e-whisper][9])
2. 配置流程
同样以 Linux + systemd 为例。
2.1 为 containerd service 设置环境变量
创建 systemd 的 drop-in 文件:
基本和docker的配置思路是一样的
sudo mkdir -p /etc/systemd/system/containerd.service.d
sudo tee /etc/systemd/system/containerd.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=http://192.168.1.1:7897"
Environment="HTTPS_PROXY=http://192.168.1.1:7897"
Environment="NO_PROXY=localhost,127.0.0.1,.svc,.cluster.local,.deployers.cn"
EOF
然后重载并重启:
sudo systemctl daemon-reload
sudo systemctl restart containerd
这一步可让 containerd 进程在启动时带上环境变量。 ([e-whisper][9])
2.2 有时候还需要编辑 config.toml(可选/扩展)
虽然代理主要通过环境变量控制,但如果你还要配置镜像仓库镜像加速/缓存(Pull-through cache),可以编辑 /etc/containerd/config.toml。例如配置镜像代理或 TLS 校验。 ([GitHub][10])
3. 测试验证
- 使用
ctr image pull docker.io/library/alpine:latest看是否能成功拉取。 - 检查 systemd 环境是否生效:
systemctl show --property=Environment containerd。 - 若仍失败,可查看 proxy 日志、containerd 日志是否有 HTTP/HTTPS 请求被拒或被阻止。
4. 常见注意事项 & 问题排查
- 与 Docker 不同,containerd 并没有一个 “daemon.json” 专门的代理配置段;推荐方式是 systemd 环境变量。 ([GitHub][11])
- 在使用 K3s 这类场景(或 Kubernetes 节点)时,可能还需要为 containerd 加上
CONTAINERD_HTTP_PROXY等前缀环境变量。 ([docs.k3s.io][5]) - 如果节点是在云环境(如 Amazon EKS 混合节点)下,需要为 containerd、kubelet、操作系统等分别配置代理。 ([Repost][12])
- 如果环境中存在 “镜像加速器” 或 “内部缓存镜像代理”(如 Nexus 或 Dragonfly 等),则还需要在 config.toml 中配置 registry.mirror 或 tls skip verify 等。 ([Cloudspinx][13])
更多推荐



所有评论(0)