gpt4 book ai didi

c# - dotnet restore 在 docker-compose build 中非常慢

转载 作者:行者123 更新时间:2023-11-30 18:11:35 25 4
gpt4 key购买 nike

我有一个 .NET Core 项目,我正在将其构建到 docker 镜像中。 Dockerfile 如下所示:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS restore
WORKDIR /tmp/build
COPY ./*.sln .
COPY ./*/*.csproj ./
# Put project files back into their own project directories
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done
RUN dotnet restore

# Copy in the source files
FROM restore AS build
WORKDIR /tmp/build
COPY . .
RUN dotnet publish -o output MyApp/MyApp.csproj

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
WORKDIR /app
COPY --from=build /tmp/build/output .
ENTRYPOINT [ "dotnet", "MyApp.dll"]

如果我 cd 进入目录并运行 dotnet restore,包会很快恢复,没有任何问题。但是,当它到达 Docker 中构建过程的 RUN dotnet restore 行时,restore 命令运行得异常缓慢,有时甚至会超时。

我只能假设这是 Docker 网络连接的某种问题,但我不知道从哪里开始。这是在 Linux 上,特别是 Deepin。

最佳答案

我相信这可能与尚未发布的 dotnet core v3 有关。

构建以下 dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS restore
WORKDIR /tmp/build
COPY . .
RUN dotnet restore

FROM restore AS build
WORKDIR /tmp/build
COPY . .
RUN dotnet publish -o output restore-example.csproj

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-alpine AS runtime
WORKDIR /app
COPY --from=build /tmp/build/output .
ENTRYPOINT [ "dotnet", "restore-example.dll"]

我的构建时间为 12 秒

$ docker system prune -f && time docker build .
Deleted Images:
deleted: sha256:af0270527dd0dd2a5e371daa395fa91f834b1573eab831725e37c1c98fcd91d7
deleted: sha256:5d5762859e232d5f010b5584d76fc5c4dcf1eb3779b50099d0844ebf08329588
deleted: sha256:906be7cd6a27a7bd5a712061faca0d02ae36cc7c1cd007e11e222f0970c7cce1
deleted: sha256:143ec3415424e99d0518cf39eaf20b5aac2f6b1c6d6edcebd788ad92cdbd14aa
deleted: sha256:2a391aaba15427d9979bc1aa57f2af03f891a055ef6e740e34f7739d64f4e593
deleted: sha256:16576c0f378d7c0450c8e9d8268e6840065e2534d8701c023075ae7b4a351a7b
deleted: sha256:ccf12511b61cab62eae3b6067cde13728d9145924e3d8266d3d765bd2014c1db
deleted: sha256:e7149dfa101cdb975b2c21fdb4f5eaa9ffb4fa5e0b720089937bce200b192353
deleted: sha256:0e1be0f142e24fac1e510ad8047ac0eee813d0583d76351bdb42cc82cbf7e8bc
deleted: sha256:8e1f0aa0f65c5bad1168eed9b9e213f6c3a2610de069e3ae6a1afc262d141eb9
deleted: sha256:85182a03f99e21e91afd9f17d373bb44d502bab3d01ff362f45cca39d6fe04d3
deleted: sha256:52ed4afff3095e3ac5b2e34a1aabce7b4407a0b95550b82657f62e56f8009585
deleted: sha256:841a8ff534c2efabee04f3e035ed7b565a5ae95eec8d16e9fdff00d7862260cb
deleted: sha256:a9aaff5c95ce5444732deef867c986cba25cd760f67bd557885d1a604c1649ab
deleted: sha256:b01c24cc26f155c7017baf482d9b7d11140cf122ca061b2241c8c5a5e734986e
deleted: sha256:863e4585da1623aa9568a58b306fb31ffaa28038b50f2354a4356f56fb9deaa6

Total reclaimed space: 67.36MB
Sending build context to Docker daemon 71.17kB
Step 1/12 : FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS restore
---> a4262c5dc8c3
Step 2/12 : WORKDIR /tmp/build
---> Running in fbf8121ee1f4
Removing intermediate container fbf8121ee1f4
---> 11d67baed4b4
Step 3/12 : COPY . .
---> 703e0db87403
Step 4/12 : RUN dotnet restore
---> Running in bc4fc4e36282
Restore completed in 6.14 sec for /tmp/build/restore-example.csproj.
Removing intermediate container bc4fc4e36282
---> e0eb73b57f9b
Step 5/12 : FROM restore AS build
---> e0eb73b57f9b
Step 6/12 : WORKDIR /tmp/build
---> Running in da50ef82948b
Removing intermediate container da50ef82948b
---> 3215dff1d63b
Step 7/12 : COPY . .
---> b5898cfbf57e
Step 8/12 : RUN dotnet publish -o output restore-example.csproj
---> Running in 14ad93b89253
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Restore completed in 41.27 ms for /tmp/build/restore-example.csproj.
restore-example -> /tmp/build/bin/Debug/netcoreapp2.2/restore-example.dll
restore-example -> /tmp/build/output/
Removing intermediate container 14ad93b89253
---> 5ec8c744e4a5
Step 9/12 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-alpine AS runtime
---> 829fb2904a07
Step 10/12 : WORKDIR /app
---> Running in a8b276c6229d
Removing intermediate container a8b276c6229d
---> 0aed3e9a3a94
Step 11/12 : COPY --from=build /tmp/build/output .
---> 36f9141d0c21
Step 12/12 : ENTRYPOINT [ "dotnet", "restore-example.dll"]
---> Running in 8e54f498b5bf
Removing intermediate container 8e54f498b5bf
---> 7bca25c5d0bb
Successfully built 7bca25c5d0bb

real 0m12.359s
user 0m0.049s
sys 0m0.050s

构建以下 dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS restore
WORKDIR /tmp/build
COPY . .
RUN dotnet restore

FROM restore AS build
WORKDIR /tmp/build
COPY . .
RUN dotnet publish -o output restore-example.csproj

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
WORKDIR /app
COPY --from=build /tmp/build/output .
ENTRYPOINT [ "dotnet", "restore-example.dll"]

我得到了 1m+ 的构建时间(并且至少有一个构建超时):

$ docker system prune -f && time docker build .
Total reclaimed space: 0B
Sending build context to Docker daemon 71.17kB
Step 1/12 : FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS restore
---> f837a0325ef5
Step 2/12 : WORKDIR /tmp/build
---> Running in a999a205fbea
Removing intermediate container a999a205fbea
---> b01c24cc26f1
Step 3/12 : COPY . .
---> 841a8ff534c2
Step 4/12 : RUN dotnet restore
---> Running in 2096ec27a007
Restore completed in 1.41 min for /tmp/build/restore-example.csproj.
Removing intermediate container 2096ec27a007
---> 85182a03f99e
Step 5/12 : FROM restore AS build
---> 85182a03f99e
Step 6/12 : WORKDIR /tmp/build
---> Running in 506bf68b37f7
Removing intermediate container 506bf68b37f7
---> 8e1f0aa0f65c
Step 7/12 : COPY . .
---> e7149dfa101c
Step 8/12 : RUN dotnet publish -o output restore-example.csproj
---> Running in 13d2b33e3145
Microsoft (R) Build Engine version 16.3.0-preview-19377-01+dd8019d9e for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Restore completed in 25.31 ms for /tmp/build/restore-example.csproj.
You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
restore-example -> /tmp/build/bin/Debug/netcoreapp2.2/restore-example.dll
restore-example -> /tmp/build/output/
Removing intermediate container 13d2b33e3145
---> 16576c0f378d
Step 9/12 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-alpine AS runtime
---> 06dbfd71b75a
Step 10/12 : WORKDIR /app
---> Running in 4fa4446bc907
Removing intermediate container 4fa4446bc907
---> 143ec3415424
Step 11/12 : COPY --from=build /tmp/build/output .
---> 5d5762859e23
Step 12/12 : ENTRYPOINT [ "dotnet", "restore-example.dll"]
---> Running in e928e1c80418
Removing intermediate container e928e1c80418
---> af0270527dd0
Successfully built af0270527dd0

real 1m31.916s
user 0m0.161s
sys 0m0.095s

我正在运行最新的 Fedora 30 安装。

Linux localhost.localdomain 5.2.9-200.fc30.x86_64 #1 SMP Fri Aug 16 21:37:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

和 docker v19

Docker version 19.03.1, build 74b1e89

关于c# - dotnet restore 在 docker-compose build 中非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com