Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet restore incredibly slow inside docker-compose build

I have a .NET Core project that I'm building into a docker image. The Dockerfile looks like this:

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"]

If I cd into the directory and run dotnet restore, the packages are restored very quickly without any problems. But when it hits the RUN dotnet restore line of the build process in Docker, the restore command operates incredibly slowly, sometimes even timing out.

I can only assume this is somehow an issue with Docker's network connection, but I have no idea where to begin. This is on Linux, specifically Deepin.

like image 598
Nick Coad Avatar asked Aug 27 '19 07:08

Nick Coad


1 Answers

I believe this may have something to do with the yet to be released dotnet core v3.

Building the following 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"]

I get a build time of 12s

$ 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

Building the following 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"]

I get a build time of 1m+ (and at least one build timed out):

$ 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

I'm running an up to date Fedora 30 install.

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

And docker v19

Docker version 19.03.1, build 74b1e89
like image 87
Brad Avatar answered Sep 23 '22 07:09

Brad