dotnet在docker-compose build内的恢复速度非常慢

时间:2019-08-27 07:15:11

标签: c# docker .net-core

我有一个要构建为Docker映像的.NET Core项目。 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上。

2 个答案:

答案 0 :(得分:2)

我相信这可能与尚未发布的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

答案 1 :(得分:0)

就我而言,它有助于在运行 `docker build` 命令之前删除 /bin/obj 文件夹。

这是我的 dockerfile,用于名为 GoD_backend 的 .NET Core 5.0 API 烘焙

GoD_builddotnet publish 作为输入的中间文件夹 GoD_backdotnet publish 放置结果的文件夹

# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
COPY /src/* /GoD_build 
RUN dotnet publish -c release -o /GoD_back /GoD_build/GoD_backend.csproj

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime:5.0
WORKDIR /GoD_build
COPY --from=build /GoD_back .
ENTRYPOINT ["dotnet", "GoD_backend.dll"]