[英]How can I reduce the build time for a .NET Core application using Docker and Kaniko?
我在我的 .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"]
我的 .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
我們使用 GitLab 和 Kaniko 來構建 gitlab-ci.yml 文件。
構建此控制台應用程序需要 7 分鍾,但使用 Go 語言編寫的另一個應用程序需要 40 秒。
如何減少此應用程序的構建時間?
您的第一行 FROM 完全未使用。 而是將您的FROM base
更改為FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim
這個問題可能是由於 Kaniko **/someDir.dockerignore 模式沒有被正確觀察到。 我注意到 /obj、/bin、.idea (rider) 和 .git 文件夾都被復制了。
https://github.com/GoogleContainerTools/kaniko/issues/1396
您也沒有使用基於 alpine 的 sdk 和運行時映像。
在dotnet restore command
中,您可以使用--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 build
構建、測試、發布和創建 beanstalk_bundle zip
Kaniko 增加了開銷,因為層緩存是遠程完成的一些存儲庫(通常)。 這一次將很大程度上取決於您如何配置 Kaniko 緩存和掛載卷。 這是我在本地機器上用於調試的東西。
#!/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.