alioth/before/hoto/docs/docker使用代理.md
2025-05-30 09:18:01 +08:00

34 lines
2.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

要在 Docker 中使用代理进行 `docker pull` 操作,你需要配置 Docker 守护进程(`dockerd`)以使用代理服务器。以下是配置步骤:
1. **创建 Docker 服务的 systemd 配置目录**
```bash
sudo mkdir -p /etc/systemd/system/docker.service.d
```
2. **创建配置文件**
`/etc/systemd/system/docker.service.d/` 目录下创建一个名为 `proxy.conf`(或任意其他名称,以 `.conf` 结尾)的配置文件,并添加以下内容:
```bash
[Service]
Environment="HTTP_PROXY=http://your_proxy_server:port/"
Environment="HTTPS_PROXY=http://your_proxy_server:port/"
Environment="NO_PROXY=localhost,127.0.0.1,your-registry.com"
```
这里 `your_proxy_server``port` 需要替换为你的代理服务器地址和端口。`NO_PROXY` 中的值用逗号分隔,可以使用通配符(*)。
3. **重新加载 systemd 配置并重启 Docker**
```bash
sudo systemctl daemon-reload
sudo systemctl restart docker
```
4. **验证配置是否生效**
使用以下命令检查 Docker 服务的环境变量是否已经正确配置:
```bash
sudo systemctl show --property=Environment docker
```
或者通过 `docker info` 命令查看配置项是否显示在输出中。
这样配置后Docker 应该能够通过代理服务器正常拉取镜像。需要注意的是Docker 服务的配置需要从 systemd 角度进行设置,而不能仅通过修改 shell 环境变量的方式。此外,如果代理服务器需要认证,可能还需要额外的配置步骤。在实际操作中,确保替换示例中的占位符(如 `proxy.example.com`)为实际的代理服务器地址。
如果你不想每次拉取镜像时都重启 Docker 服务,可以考虑使用 Privoxy 作为本地代理服务器,这样可以通过控制 Privoxy 的启动和停止来间接控制 Docker 使用代理。这种方法的好处是不需要频繁重启 Docker 服务,可以更加灵活地控制代理的使用。具体步骤可以参考相关博客文章的描述。