docker-compose 使用外部宿主机的代理的设置

5 min read

在 Docker Compose 中使用外部宿主机的代理设置有两种方法。

方法一:在 Docker Compose 文件中指定环境变量

在 Docker Compose 文件中指定 HTTP_PROXY 和 HTTPS_PROXY 环境变量,并将它们设置为宿主机的代理地址,如下所示:

version: "3"
services:
  myapp:
    image: myapp
    environment:
      - HTTP_PROXY=http://host:port
      - HTTPS_PROXY=http://host:port

方法二:使用默认的 Docker Compose 环境变量

Docker Compose 支持一些默认的环境变量,用于配置 Docker 服务和容器的网络连接。可以利用这些环境变量来设置代理。

在 Linux 系统上,通过设置 http_proxy 和 https_proxy 环境变量来为 Docker 守护程序设置代理。这些环境变量将自动传递给 Compose 运行的所有容器。例如:

export http_proxy=http://host:port
export https_proxy=http://host:port
docker-compose up

在 Windows 系统上,可以通过在 PowerShell 中设置环境变量来为 Docker 守护程序设置代理。例如:

$Env:http_proxy="http://host:port"
$Env:https_proxy="http://host:port"
docker-compose up

使用这些默认的环境变量可以为 Docker Compose 中的所有服务设置代理,而无需在每个服务中指定它们。