C# 如何使用Docker和Kaniko缩短.NET核心应用程序的构建时间?

C# 如何使用Docker和Kaniko缩短.NET核心应用程序的构建时间?,c#,docker,.net-core,dockerfile,kaniko,C#,Docker,.net Core,Dockerfile,Kaniko,在我的.NETCore2.2控制台应用程序中有以下Dockerfile FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base WORKDIR /app FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build WORKDIR /src COPY ["TaikunBillerPoller.csproj", ""] R

在我的.NETCore2.2控制台应用程序中有以下Dockerfile

FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["TaikunBillerPoller.csproj", ""]
RUN dotnet restore "TaikunBillerPoller.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "TaikunBillerPoller.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "TaikunBillerPoller.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TaikunBillerPoller.dll"]
我的.dockrignore文件看起来像

**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.vs
**/.vscode
**/*.*proj.user
**/azds.yaml
**/charts
**/bin
**/obj
**/Dockerfile
**/Dockerfile.develop
**/docker-compose.yml
**/docker-compose.*.yml
**/*.dbmdl
**/*.jfm
**/secrets.dev.yaml
**/values.dev.yaml
**/.toolstarget
我们正在使用GitLab和Kaniko构建GitLab-ci.yml文件

构建此控制台应用程序需要7分钟,但另一个用Go语言编写的应用程序需要40秒


如何缩短此应用程序的构建时间?

您的第一行FROM完全未使用。而是将
从base
行更改为
从mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim

这个问题可能是由于Kaniko**/someDir.dockrignore模式没有得到正确的观察。我注意到/obj、/bin、.idea(rider)和.git文件夹都在被复制

您也没有使用基于alpine的sdk和运行时映像

dotnet restore命令中
可以使用
--no cache
标志,因为docker层缓存将处理该问题

dotnet publish
执行生成,因此您可以跳过调用
dotnet build
。如果要执行测试,可以调用
dotnet test

您正在显式调用
dotnet restore
,因此在所有后续的
dotnet
命令中,您可以使用
--no restore
选项

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS base
#Add whatever tools you need to the base image
RUN apk add --update --no-cache git bash curl zip; \
    export PATH="$PATH:/root/.dotnet/tools"; \
    dotnet tool install --global dotnet-xunit-to-junit --version 1.0.2

FROM base AS restore
WORKDIR /src
COPY ["TaikunBillerPoller.csproj", ""]
RUN dotnet restore --no-cache "TaikunBillerPoller.csproj"
COPY . .

FROM restore as publish
ARG VERSION="0.0.0"
RUN dotnet test "TaikunBillerPoller.csproj" --configuration Release --no-restore
RUN dotnet publish "TaikunBillerPoller.csproj" --output /app --configuration Release --no-restore /p:Version=$VERSION

FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TaikunBillerPoller.dll"]
在2015 Mac上,我有一个asp.net微服务,它使用正常的
docker构建
构建、测试、发布和创建beanstalk_捆绑包zip,时间如下:

  • 51s没有缓存
  • 22s代码更改

  • 您的第一个FROM线路完全未使用。而是将
    从base
    行更改为
    从mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim

    这个问题可能是由于Kaniko**/someDir.dockrignore模式没有得到正确的观察。我注意到/obj、/bin、.idea(rider)和.git文件夹都在被复制

    您也没有使用基于alpine的sdk和运行时映像

    dotnet restore命令中
    可以使用
    --no cache
    标志,因为docker层缓存将处理该问题

    dotnet publish
    执行生成,因此您可以跳过调用
    dotnet build
    。如果要执行测试,可以调用
    dotnet test

    您正在显式调用
    dotnet restore
    ,因此在所有后续的
    dotnet
    命令中,您可以使用
    --no restore
    选项

    FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS base
    #Add whatever tools you need to the base image
    RUN apk add --update --no-cache git bash curl zip; \
        export PATH="$PATH:/root/.dotnet/tools"; \
        dotnet tool install --global dotnet-xunit-to-junit --version 1.0.2
    
    FROM base AS restore
    WORKDIR /src
    COPY ["TaikunBillerPoller.csproj", ""]
    RUN dotnet restore --no-cache "TaikunBillerPoller.csproj"
    COPY . .
    
    FROM restore as publish
    ARG VERSION="0.0.0"
    RUN dotnet test "TaikunBillerPoller.csproj" --configuration Release --no-restore
    RUN dotnet publish "TaikunBillerPoller.csproj" --output /app --configuration Release --no-restore /p:Version=$VERSION
    
    FROM mcr.microsoft.com/dotnet/core/runtime:2.2-alpine AS final
    WORKDIR /app
    COPY --from=publish /app .
    ENTRYPOINT ["dotnet", "TaikunBillerPoller.dll"]
    
    在2015 Mac上,我有一个asp.net微服务,它使用正常的
    docker构建
    构建、测试、发布和创建beanstalk_捆绑包zip,时间如下:

    • 51s没有缓存
    • 22s代码更改

    • 如果您试图在容器中使用Microsoft软件,就会发生这种情况。如果go应用程序位于alpine上,它将生成一个大小为几百KB的容器。MS应用程序必须大于2GB。这将需要很长时间才能在网络中移动,更不用说构建了。MS速度慢且臃肿,应该不惜一切代价避免:)话虽如此,7分钟还不错——这是一个真正的操作问题吗?@SoftwareEngineer我不知道使用docker.hub的一些小图像是否可能缩短时间。可能我使用的图像不正确。如果你试图在容器中使用Microsoft软件,就会发生这种情况。如果go应用程序位于alpine上,它将生成一个大小为几百KB的容器。MS应用程序必须大于2GB。这将需要很长时间才能在网络中移动,更不用说构建了。MS速度慢且臃肿,应该不惜一切代价避免:)话虽如此,7分钟还不错——这是一个真正的操作问题吗?@SoftwareEngineer我不知道使用docker.hub的一些小图像是否可能缩短时间。也许我使用的图像不正确。