我通过 WSL(适用于 Linux 的 Windows 子系统)在 Windows 上使用 Docker。当我直接使用 Docker Desktop 时,设置代理对于 docker run 和 docker build 命令都运行良好。但是,当在 Ubuntu 环境中通过 WSL 使用 Docker 时,代理设置似乎不会在 docker 构建过程中应用。
我通过 WSL(适用于 Linux 的 Windows 子系统)在 Windows 上使用 Docker。我在几个地方配置了代理设置:
/etc/environment
http_proxy=http://username:[email protected]:3128
https_proxy=http://username:[email protected]:3128
/etc/docker/daemon.json
{
"proxies": {
"http-proxy": "http://username:[email protected]:3128",
"https-proxy": "http://username:[email protected]:3128",
"no-proxy": "localhost,127.0.0.1,docker-registry.somecorporation.com"
},
"insecure-registries": ["mcr.microsoft.com"]
}
/etc/systemd/system/docker.service.d/proxy.conf
[Service]
Environment="http_proxy=http://username:[email protected]:3128"
Environment="https_proxy=http://username:[email protected]:3128"
Environment="no_proxy=localhost,127.0.0.1,docker-registry.somecorporation.com"
代理可以在 WSL 中与 apt 和 docker pull 命令一起正常工作。但是,当我运行 docker build 时,Dockerfile 中的 apt 命令不使用代理设置。
这是我的 dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt-get update && \
apt-get install -y libldap2-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["EQP_Workspace/EQP_Workspace.csproj", "EQP_Workspace/"]
RUN dotnet restore "EQP_Workspace/EQP_Workspace.csproj"
COPY . .
WORKDIR "/src/EQP_Workspace"
RUN dotnet build "EQP_Workspace.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "EQP_Workspace.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "EQP_Workspace.dll"]
还有这个错误消息
[eqp_workspace base 3/3] RUN apt-get update && apt-get install -y libldap2-dev && apt-get clean && rm -rf /var/lib/apt/lists/*:
30.69 Ign:1 http://deb.debian.org/debian bookworm InRelease
30.70 Ign:2 http://deb.debian.org/debian bookworm-updates InRelease
30.70 Ign:3 http://deb.debian.org/debian-security bookworm-security InRelease
31.70 Ign:1 http://deb.debian.org/debian bookworm InRelease
31.70 Ign:2 http://deb.debian.org/debian bookworm-updates InRelease
31.71 Ign:3 http://deb.debian.org/debian-security bookworm-security InRelease
33.70 Ign:1 http://deb.debian.org/debian bookworm InRelease
33.71 Ign:2 http://deb.debian.org/debian bookworm-updates InRelease
33.71 Ign:3 http://deb.debian.org/debian-security bookworm-security InRelease
37.70 Err:1 http://deb.debian.org/debian bookworm InRelease
37.70 Could not connect to deb.debian.org:80 (199.232.46.132), connection timed out
37.71 Err:2 http://deb.debian.org/debian bookworm-updates InRelease
37.71 Unable to connect to deb.debian.org:80:
37.71 Err:3 http://deb.debian.org/debian-security bookworm-security InRelease
37.71 Unable to connect to deb.debian.org:80:
37.71 Reading package lists...
37.72 W: Failed to fetch http://deb.debian.org/debian/dists/bookworm/InRelease Could not connect to deb.debian.org:80 (199.232.46.132), connection timed out
37.72 W: Failed to fetch http://deb.debian.org/debian/dists/bookworm-updates/InRelease Unable to connect to deb.debian.org:80:
37.72 W: Failed to fetch http://deb.debian.org/debian-security/dists/bookworm-security/InRelease Unable to connect to deb.debian.org:80:
37.72 W: Some index files failed to download. They have been ignored, or old ones used instead.
37.72 Reading package lists...
37.73 Building dependency tree...
37.73 Reading state information...
37.73 E: Unable to locate package libldap2-dev
------
time="2024-07-24T14:59:07+08:00" level=warning msg="current commit information was not captured by the build" error="git was not found in the system: exec: \"git.exe\": executable file not found in %PATH%"
failed to solve: process "/bin/sh -c apt-get update && apt-get install -y libldap2-dev && apt-get clean && rm -rf /var/lib/apt/lists/*" did not complete successfully: exit code: 100
我需要确保在 WSL 中的 docker 构建过程中也使用 Docker Desktop 中应用的代理设置。
如何确保 Docker 构建命令正确使用配置的代理设置?
docker pull
将使用守护程序中的代理设置,但 docker build
需要为 Docker CLI 设置代理。
我在 Windows 上的 ~/.docker/config.json 文件中配置了代理,并且它有效。
{
"proxies": {
"default": {
"httpProxy": "http://username:[email protected]:3128",
"httpsProxy": "http://username:[email protected]:3128"
}
}
}