简体   繁体   English

如何使用 Docker 和 Kaniko 减少 .NET 核心应用程序的构建时间?

[英]How can I reduce the build time for a .NET Core application using Docker and Kaniko?

I have following Dockerfile in my .NET Core 2.2 console application.我在我的 .NET Core 2.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"]

My.dockerignore file looks like我的 .dockerignore 文件看起来像

**/.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

We are using GitLab and Kaniko for building gitlab-ci.yml file.我们使用 GitLab 和 Kaniko 来构建 gitlab-ci.yml 文件。

This console application takes 7 minutes to build, but another application written in the Go language takes 40 seconds.构建此控制台应用程序需要 7 分钟,但使用 Go 语言编写的另一个应用程序需要 40 秒。

How might I reduce the build time for this application?如何减少此应用程序的构建时间?

Your first FROM line is completely unused.您的第一行 FROM 完全未使用。 Instead change your FROM base line to FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim而是将您的FROM base更改为FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim

This issue may be due to the fact that Kaniko **/someDir.dockerignore patterns are not properly observed.这个问题可能是由于 Kaniko **/someDir.dockerignore 模式没有被正确观察到。 I'm noticing that /obj, /bin, .idea (rider) and.git folders are all being copied.我注意到 /obj、/bin、.idea (rider) 和 .git 文件夹都被复制了。

https://github.com/GoogleContainerTools/kaniko/issues/1396 https://github.com/GoogleContainerTools/kaniko/issues/1396

You are also not using the alpine based sdk and runtime images.您也没有使用基于 alpine 的 sdk 和运行时映像。

In the dotnet restore command you can use the --no-cache flag because docker layer cacheing will take care of that.dotnet restore command中,您可以使用--no-cache标志,因为 docker 层缓存会解决这个问题。

dotnet publish does a build so you can skip calling dotnet build . dotnet publish进行构建,因此您可以跳过调用dotnet build If you want to perform testing you can call dotnet test then如果要执行测试,可以调用dotnet test然后

You are explicitly calling dotnet restore so in all subsequent dotnet commands you can use the --no-restore option.您正在显式调用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"]

On a 2015 Mac I have an asp.net microservice that builds, tests, publishes and creates a beanstalk_bundle zip using a normal docker build with the following times:在 2015 Mac 上,我有一个 asp.net 微服务,它使用普通的docker build构建、测试、发布和创建 beanstalk_bundle zip

  • 51s No cache 51s 无缓存
  • 22s Code change 22s 代码更改
  • <1s No code change (pipeline yml change) <1s 无代码更改(管道 yml 更改)

Kaniko adds overhead because layer caching is done remotely to some repository (typically). Kaniko 增加了开销,因为层缓存是远程完成的一些存储库(通常)。 This time is going to depend a lot on how you have your Kaniko cache and mounted volumes configured.这一次将很大程度上取决于您如何配置 Kaniko 缓存和挂载卷。 Here is something I use on my local machine for debugging.这是我在本地机器上用于调试的东西。

#!/bin/bash
# Assuming this is either not an ephemeral machine, or the ephemeral machine
# maps the cache directory to permanent volume.
# We cache images into the local machine
# so that the Kaniko container, which is ephemeral, does not have to pull them each time.
docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest \
        --cache-dir=/workspace/cache \
        --image=mcr.microsoft.com/dotnet/core/sdk:2.2-alpine \
        --image=mcr.microsoft.com/dotnet/core/aspnet:2.2-alpine
docker run -it --rm \
        -v `pwd`:/workspace \
        -v `pwd`/kaniko-config.json:/kaniko/.docker/config.json:ro \
        -v `pwd`/reports:/reports \
        -v `pwd`/beanstalk_bundle:/beanstalk_bundle \
        gcr.io/kaniko-project/executor:latest \
        --dockerfile "buildTestPublish.Dockerfile" \
        --destination "registry.gitlab.com/somePath/theImageName:theVersion" \
        --skip-unused-stages \
        --cache \
        --cache-dir=/workspace/cache \
        --verbosity=trace

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker如何构建.Net核心应用? - How to build .Net core application in Docker? 我可以进一步减少对由docker容器上的asp.net Web API应用程序运行的docker映像的依赖性 - Can I further reduce dependency on docker images to run by asp.net web api application on docker container 我可以使用Microsoft Dotnet CLI构建ASP.NET Core Web应用程序吗? - Can I build ASP.NET Core web application using Microsoft Dotnet CLI? 如何构建小型 dotnet 核心应用 docker 图像 - How I can build small dotnet core app docker images 如何使用 EF Core 3.1.5 解决 ASP.net Core 应用程序中的 null 引用异常? - How can I resolve null reference exception in ASP.net Core application using EF Core 3.1.5? 如何在 Mac 和 Windows 上部署 .NET Framework 应用程序? 如何构建跨平台(windows 和 MacOS).NET Core? - How can I deploy .NET Framework application on Mac and Windows both? How to Build Cross-Platform(windows and MacOS) .NET Core? 如何在 .net 核心应用程序中增加超时。 504 http 响应 in.Net Core 应用 - How can I increase timeout in .net core application. 504 http Response in .Net Core application 如何在 ASP.NET 核心应用程序中使用 BackgroundService 每年执行一个方法? - How can I execute a method every year using BackgroundService in ASP.NET core Application? 我怎样才能端到端地测试这个 .net 核心控制台应用程序? - How can I end to end test this .net core console application? 如何将 .NET 核心应用程序连接到 wsdl 服务 - How can I connect .NET Core application to wsdl service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM