构建docker镜像时使用代理服务器

背景介绍

最近需要在公司内网通过docker部署一个.net core 应用,在花费一些时间解决内网服务器访问外网的问题后,我把代码放到服务器上,并且准备通过docker-cli构建应用镜像,但当构建步骤到restore时,命令一直卡着,原来是docker build时没有走系统代理。

Dockerfile

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY  ./ ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
RUN ddotnet ef database update -p .\ProjectFolder\ProjectFile.csproj

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "ProjectFile.dll"]

FROM microsoft/dotnet:aspnetcore-runtime

执行过程

>> docker build .

输出结果

Step 1/13 : FROM microsoft/dotnet:sdk AS build-env
—> 08657316a4cd
Step 2/13 : WORKDIR /app
—> Using cache
—> 5ec5d1f4f275
Step 3/13 : COPY ./ ./
—> 733eb7bbdaa7
Step 5/13 : RUN dotnet restore
//卡在此处

修改方案

使用以下命令代替

 docker build --build-arg http_proxy=http://proxy_address:proxy_port --build-arg https_proxy=http://proxy_address:proxy_port .

修改后输出

Sending build context to Docker daemon 1.044MB
Step 1/12 : FROM microsoft/dotnet:sdk AS build-env
—> 08657316a4cd
Step 2/12 : WORKDIR /app
—> Using cache
—> 5ec5d1f4f275
Step 3/12 : COPY ./ ./
—> ed9be3bd8315
Step 4/12 : RUN dotnet restore
—> Running in dd57897dac80
Restore completed in 24.38 sec for /app/ProjectFolder/ProjectFile.csproj.
Removing intermediate container dd57897dac80
—> c8cc22c13b64
Step 5/12 : COPY . ./
—> 8e79de63d907
Step 6/12 : RUN dotnet publish -c Release -o out
—> Running in e39e1c5d411c
Microsoft ® Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright © Microsoft Corporation. All rights reserved.

大功告成!

相关文章

利用Fiddler作为网络代理
通过网络代理进行内网服务器的docker安装及镜像下载

你可能感兴趣的:(计算机网络)