Docker 中的代理

时间:2025-05-12 07:22:57

docker 中的代理设置分为两类:docker 使用代理访问网络;docker container 使用代理访问网络。因此要注意区分。

使用代理下载镜像

第一种情况比较适合当下不能直接访问docker官方镜像库的情况。

# 创建配置文件;设置是针对 daemon,因为所有具体的操作都是通过 daemon 进行
sudo vim /etc/systemd/system//

# 输入以下内容
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:8123"
Environment="HTTPS_PROXY=http://127.0.0.1:8123"

# 重启服务
sudo systemctl daemon-reload
sudo systemctl restart docker
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

设置容器中的代理

全局设置

全局设置有两种方法,一种是修改 /etc/docker/;另一种是设置 <HOME>/.docker/

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://172.17.0.1:8123",
     "httpsProxy": "http://172.17.0.1:8123",
     "noProxy": "localhost,127.0.0.1,."
   }
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

局部设置

单独设置 container 代理的最简单方法是使用 docker-compose.yml。在其中指定 HTTP_PROXY 等环境变量。

另一种方法是构建镜像时,配置好这些环境变量。

docker build --build-arg http_proxy=http://172.17.0.1:8123 --build-arg https_proxy=http://172.17.0.1:8123 -t image_name .
  • 1