Skip to content

Restore/build happens twice #169

Open

Description

@Leon99

It looks like restore/build happen twice, first time initiated by

RUN dotnet restore ...
RUN dotnet build ...

and then again by RUN dotnet publish
Is there a way to avoid doing it twice so our CI builds finish quicker?

Activity

  1. bwateratmsft commented on Mar 12, 2019

    @bwateratmsft
    Contributor

    Leon V (@Leon99), is it actually restoring and building twice? When I tested it "says" it's restoring again, but it's "skipped" since everything is already restored, and it doesn't mention anything about building--it's just publishing to the /app directory.

    You might try removing the "dotnet restore" and "dotnet build" commands from the Dockerfile, however, the reason we separate the three is so that Docker's image build caching works more effectively when doing Release builds in VS. In particular, we can avoid re-restoring if the project file has not changed. However, if you're only ever using Debug builds in VS this doesn't matter.

  2. Leon99 commented on Mar 15, 2019

    @Leon99
    Author

    Brandon Waterloo (@bwateratmsft) I tried RUN dotnet publish ... --no-build and CI build resulted in an error, which made me think that it actually needs to build again for some reason. I tried removing dotnet restore/build as well, which also ended with another error.

  3. bwateratmsft commented on Mar 15, 2019

    @bwateratmsft
    Contributor

    Leon V (@Leon99) If possible can you share your dockerfile? You can change names / redact as needed.

  4. Leon99 commented on Apr 1, 2019

    @Leon99
    Author

    Brandon Waterloo (@bwateratmsft) we decided to postpone migration to Docker for now, but those were unaltered docker files generated by the latest VS Docker Tools.

  5. cwe1ss commented on Jul 26, 2019

    @cwe1ss

    We have the same behavior. Restore happens 3 times and build twice. The subsequent restore/build calls are a bit faster, but still, it results in slower CI builds. It would be great if, at least, the publish step would use the already built bits from the build step. We also get an error if we add --no-build to dotnet publish.

    Here's our Dockerfile. It has been auto-generated by VS. We use a mono-repo with direct project references to infrastructure projects and DTO/Contracts projects of other contexts/microervices.

    Dockerfile
    FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
    WORKDIR /src
    COPY ["Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj", "Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/"]
    COPY ["Contexts/Permissions/MyCompany.Permissions.Core/MyCompany.Permissions.Core.csproj", "Contexts/Permissions/MyCompany.Permissions.Core/"]
    COPY ["Contexts/Permissions/MyCompany.Permissions.Contracts/MyCompany.Permissions.Contracts.csproj", "Contexts/Permissions/MyCompany.Permissions.Contracts/"]
    COPY ["Infrastructure/MyCompany.Infrastructure.Contracts/MyCompany.Infrastructure.Contracts.csproj", "Infrastructure/MyCompany.Infrastructure.Contracts/"]
    COPY ["Infrastructure/MyCompany.Infrastructure.Meceqs/MyCompany.Infrastructure.Meceqs.csproj", "Infrastructure/MyCompany.Infrastructure.Meceqs/"]
    COPY ["Infrastructure/MyCompany.Infrastructure/MyCompany.Infrastructure.csproj", "Infrastructure/MyCompany.Infrastructure/"]
    COPY ["Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/MyCompany.Infrastructure.Data.EntityFrameworkCore.csproj", "Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/"]
    COPY ["Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/MyCompany.Infrastructure.Hosts.AspNetCore.csproj", "Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/"]
    COPY ["Infrastructure/MyCompany.Infrastructure.Hosts/MyCompany.Infrastructure.Hosts.csproj", "Infrastructure/MyCompany.Infrastructure.Hosts/"]
    COPY ["Contexts/Permissions/MyCompany.Permissions.Client/MyCompany.Permissions.Client.csproj", "Contexts/Permissions/MyCompany.Permissions.Client/"]
    RUN dotnet restore "Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj"
    COPY . .
    WORKDIR "/src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi"
    RUN dotnet build "MyCompany.Permissions.Hosts.WebApi.csproj" -c Release -o /app
    
    FROM build AS publish
    RUN dotnet publish "MyCompany.Permissions.Hosts.WebApi.csproj" -c Release -o /app
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app .
    ENTRYPOINT ["dotnet", "MyCompany.Permissions.Hosts.WebApi.dll"]
    

    And here's the docker build output:

    docker build
    docker build -f .\Contexts\Permissions\MyCompany.Permissions.Hosts.WebApi\Dockerfile -t permissions-webapi .                                                                                                                                     Sending build context to Docker daemon  236.7MB
    Step 1/25 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
     ---> f6d51449c477
    Step 2/25 : WORKDIR /app
     ---> Running in 1a017048e51c
    Removing intermediate container 1a017048e51c
     ---> e8d160215a80
    Step 3/25 : EXPOSE 80
     ---> Running in 3dfdf129c8ea
    Removing intermediate container 3dfdf129c8ea
     ---> 6e6dbf193187
    Step 4/25 : FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
     ---> e4747ec2aaff
    Step 5/25 : WORKDIR /src
     ---> Running in bd9fd7a3e20e
    Removing intermediate container bd9fd7a3e20e
     ---> 213fe329281a
    Step 6/25 : COPY ["Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj", "Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/"]
     ---> 736e4108c95f
    Step 7/25 : COPY ["Contexts/Permissions/MyCompany.Permissions.Core/MyCompany.Permissions.Core.csproj", "Contexts/Permissions/MyCompany.Permissions.Core/"]
     ---> 9148932bc86c
    Step 8/25 : COPY ["Contexts/Permissions/MyCompany.Permissions.Contracts/MyCompany.Permissions.Contracts.csproj", "Contexts/Permissions/MyCompany.Permissions.Contracts/"]
     ---> 9c8d1d7603a1
    Step 9/25 : COPY ["Infrastructure/MyCompany.Infrastructure.Contracts/MyCompany.Infrastructure.Contracts.csproj", "Infrastructure/MyCompany.Infrastructure.Contracts/"]
     ---> e29c7f4c7d7e
    Step 10/25 : COPY ["Infrastructure/MyCompany.Infrastructure.Meceqs/MyCompany.Infrastructure.Meceqs.csproj", "Infrastructure/MyCompany.Infrastructure.Meceqs/"]
     ---> 6188c131b978
    Step 11/25 : COPY ["Infrastructure/MyCompany.Infrastructure/MyCompany.Infrastructure.csproj", "Infrastructure/MyCompany.Infrastructure/"]
     ---> a41876842336
    Step 12/25 : COPY ["Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/MyCompany.Infrastructure.Data.EntityFrameworkCore.csproj", "Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/"]
     ---> 79b9200cc8ec
    Step 13/25 : COPY ["Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/MyCompany.Infrastructure.Hosts.AspNetCore.csproj", "Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/"]
     ---> d6267ed03940
    Step 14/25 : COPY ["Infrastructure/MyCompany.Infrastructure.Hosts/MyCompany.Infrastructure.Hosts.csproj", "Infrastructure/MyCompany.Infrastructure.Hosts/"]
     ---> 86fbc3468182
    Step 15/25 : COPY ["Contexts/Permissions/MyCompany.Permissions.Client/MyCompany.Permissions.Client.csproj", "Contexts/Permissions/MyCompany.Permissions.Client/"]
     ---> b2b902f30c95
    Step 16/25 : RUN dotnet restore "Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj"
     ---> Running in 8f79f54f0026
      Restore completed in 10.65 sec for /src/Contexts/Permissions/MyCompany.Permissions.Contracts/MyCompany.Permissions.Contracts.csproj.
      Restore completed in 5.63 sec for /src/Contexts/Permissions/MyCompany.Permissions.Core/MyCompany.Permissions.Core.csproj.
      Restore completed in 16.4 sec for /src/Contexts/Permissions/MyCompany.Permissions.Client/MyCompany.Permissions.Client.csproj.
      Restore completed in 8.37 ms for /src/Infrastructure/MyCompany.Infrastructure.Contracts/MyCompany.Infrastructure.Contracts.csproj.
      Restore completed in 56.12 ms for /src/Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/MyCompany.Infrastructure.Data.EntityFrameworkCore.csproj.
      Restore completed in 9.19 sec for /src/Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/MyCompany.Infrastructure.Hosts.AspNetCore.csproj.
      Restore completed in 9.4 sec for /src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj.
      Restore completed in 30.14 ms for /src/Infrastructure/MyCompany.Infrastructure.Meceqs/MyCompany.Infrastructure.Meceqs.csproj.
      Restore completed in 21.16 ms for /src/Infrastructure/MyCompany.Infrastructure/MyCompany.Infrastructure.csproj.
      Restore completed in 144.13 ms for /src/Infrastructure/MyCompany.Infrastructure.Hosts/MyCompany.Infrastructure.Hosts.csproj.
    Removing intermediate container 8f79f54f0026
     ---> d7b853733b11
    Step 17/25 : COPY . .
     ---> 8730444f1a05
    Step 18/25 : WORKDIR "/src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi"
     ---> Running in bcf22a6fa1a4
    Removing intermediate container bcf22a6fa1a4
     ---> 0e0be8eef2ab
    Step 19/25 : RUN dotnet build "MyCompany.Permissions.Hosts.WebApi.csproj" -c Release -o /app
     ---> Running in 0270a15ce6ba
    Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restore completed in 336.09 ms for /src/Contexts/Permissions/MyCompany.Permissions.Contracts/MyCompany.Permissions.Contracts.csproj.
      Restore completed in 178.81 ms for /src/Contexts/Permissions/MyCompany.Permissions.Core/MyCompany.Permissions.Core.csproj.
      Restore completed in 660.58 ms for /src/Contexts/Permissions/MyCompany.Permissions.Client/MyCompany.Permissions.Client.csproj.
      Restore completed in 28.31 ms for /src/Infrastructure/MyCompany.Infrastructure.Contracts/MyCompany.Infrastructure.Contracts.csproj.
      Restore completed in 72.25 ms for /src/Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/MyCompany.Infrastructure.Data.EntityFrameworkCore.csproj.
      Restore completed in 799.3 ms for /src/Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/MyCompany.Infrastructure.Hosts.AspNetCore.csproj.
      Restore completed in 174.16 ms for /src/Infrastructure/MyCompany.Infrastructure.Hosts/MyCompany.Infrastructure.Hosts.csproj.
      Restore completed in 1.27 sec for /src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj.
      Restore completed in 68.84 ms for /src/Infrastructure/MyCompany.Infrastructure.Meceqs/MyCompany.Infrastructure.Meceqs.csproj.
      Restore completed in 37.1 ms for /src/Infrastructure/MyCompany.Infrastructure/MyCompany.Infrastructure.csproj.
      MyCompany.Infrastructure.Contracts -> /app/MyCompany.Infrastructure.Contracts.dll
      MyCompany.Permissions.Contracts -> /app/MyCompany.Permissions.Contracts.dll
      MyCompany.Infrastructure -> /app/MyCompany.Infrastructure.dll
      MyCompany.Infrastructure.Meceqs -> /app/MyCompany.Infrastructure.Meceqs.dll
      MyCompany.Infrastructure.Hosts -> /app/MyCompany.Infrastructure.Hosts.dll
      MyCompany.Permissions.Client -> /app/MyCompany.Permissions.Client.dll
      MyCompany.Infrastructure.Hosts.AspNetCore -> /app/MyCompany.Infrastructure.Hosts.AspNetCore.dll
      MyCompany.Infrastructure.Data.EntityFrameworkCore -> /app/MyCompany.Infrastructure.Data.EntityFrameworkCore.dll
      MyCompany.Permissions.Core -> /app/MyCompany.Permissions.Core.dll
      MyCompany.Permissions.Hosts.WebApi -> /app/MyCompany.Permissions.Hosts.WebApi.dll
    
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:13.00
    Removing intermediate container 0270a15ce6ba
     ---> f434dcc36a22
    Step 20/25 : FROM build AS publish
     ---> f434dcc36a22
    Step 21/25 : RUN dotnet publish "MyCompany.Permissions.Hosts.WebApi.csproj" -c Release -o /app
     ---> Running in 64f630058353
    Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restore completed in 59.12 ms for /src/Contexts/Permissions/MyCompany.Permissions.Client/MyCompany.Permissions.Client.csproj.
      Restore completed in 56.61 ms for /src/Contexts/Permissions/MyCompany.Permissions.Contracts/MyCompany.Permissions.Contracts.csproj.
      Restore completed in 10.67 ms for /src/Contexts/Permissions/MyCompany.Permissions.Core/MyCompany.Permissions.Core.csproj.
      Restore completed in 1.33 ms for /src/Infrastructure/MyCompany.Infrastructure.Contracts/MyCompany.Infrastructure.Contracts.csproj.
      Restore completed in 3.8 ms for /src/Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/MyCompany.Infrastructure.Data.EntityFrameworkCore.csproj.
      Restore completed in 20.41 ms for /src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/MyCompany.Permissions.Hosts.WebApi.csproj.
      Restore completed in 6.76 ms for /src/Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/MyCompany.Infrastructure.Hosts.AspNetCore.csproj.
      Restore completed in 13.05 ms for /src/Infrastructure/MyCompany.Infrastructure.Meceqs/MyCompany.Infrastructure.Meceqs.csproj.
      Restore completed in 1.7 ms for /src/Infrastructure/MyCompany.Infrastructure/MyCompany.Infrastructure.csproj.
      Restore completed in 3.94 ms for /src/Infrastructure/MyCompany.Infrastructure.Hosts/MyCompany.Infrastructure.Hosts.csproj.
      MyCompany.Infrastructure.Contracts -> /src/Infrastructure/MyCompany.Infrastructure.Contracts/bin/Release/netstandard2.0/MyCompany.Infrastructure.Contracts.dll
      MyCompany.Infrastructure -> /src/Infrastructure/MyCompany.Infrastructure/bin/Release/netstandard2.0/MyCompany.Infrastructure.dll
      MyCompany.Permissions.Contracts -> /src/Contexts/Permissions/MyCompany.Permissions.Contracts/bin/Release/netstandard2.0/MyCompany.Permissions.Contracts.dll
      MyCompany.Infrastructure.Meceqs -> /src/Infrastructure/MyCompany.Infrastructure.Meceqs/bin/Release/netstandard2.0/MyCompany.Infrastructure.Meceqs.dll
      MyCompany.Infrastructure.Hosts -> /src/Infrastructure/MyCompany.Infrastructure.Hosts/bin/Release/netstandard2.0/MyCompany.Infrastructure.Hosts.dll
      MyCompany.Permissions.Client -> /src/Contexts/Permissions/MyCompany.Permissions.Client/bin/Release/netstandard2.0/MyCompany.Permissions.Client.dll
      MyCompany.Infrastructure.Hosts.AspNetCore -> /src/Infrastructure/MyCompany.Infrastructure.Hosts.AspNetCore/bin/Release/netstandard2.0/MyCompany.Infrastructure.Hosts.AspNetCore.dll
      MyCompany.Infrastructure.Data.EntityFrameworkCore -> /src/Infrastructure/MyCompany.Infrastructure.Data.EntityFrameworkCore/bin/Release/netstandard2.0/MyCompany.Infrastructure.Data.EntityFrameworkCore.dll
      MyCompany.Permissions.Core -> /src/Contexts/Permissions/MyCompany.Permissions.Core/bin/Release/netstandard2.0/MyCompany.Permissions.Core.dll
      MyCompany.Permissions.Hosts.WebApi -> /src/Contexts/Permissions/MyCompany.Permissions.Hosts.WebApi/bin/Release/netcoreapp2.2/MyCompany.Permissions.Hosts.WebApi.dll
      MyCompany.Permissions.Hosts.WebApi -> /app/
    Removing intermediate container 64f630058353
     ---> 8011f8d00e97
    Step 22/25 : FROM base AS final
     ---> 6e6dbf193187
    Step 23/25 : WORKDIR /app
     ---> Running in c8b00bfadc51
    Removing intermediate container c8b00bfadc51
     ---> 566ae8c6c80a
    Step 24/25 : COPY --from=publish /app .
     ---> 8cc5b7304abf
    Step 25/25 : ENTRYPOINT ["dotnet", "MyCompany.Permissions.Hosts.WebApi.dll"]
     ---> Running in d9fb9d486d47
    Removing intermediate container d9fb9d486d47
     ---> 4757507ae4a9
    Successfully built 4757507ae4a9
    Successfully tagged permissions-webapi:latest
    
  6. cwe1ss commented on Jan 31, 2021

    @cwe1ss

    I gave Docker another go today and I ran into the same problem again. However, today I figured out that the reason for restoring twice is that we use a "Directory.Build.props" and a "Directory.Packages.props" (in preview, I know). Since those two are not included in the first copy step, the first restore yields different results.

    After adding COPY steps for those two files, it worked as expected.

    Not sure what a good solution for this would be. It's unfortunate that the Dockerfile needs a static list of things to copy for something that isn't really static. A "Directory.Build.props"-file could include another props-file from somewhere else, etc and any change to those files that isn't replicated to each affected Dockerfile might result in a broken/unintended Docker image.

  7. lvde0 commented on Jun 5, 2025

    @lvde0

    Same issue. With the default ASP.NET Dockerfile restoring happens 3 (!!!) times and building 2 times. That makes the entire build really slow.

  8. patverb commented on Jun 9, 2025

    @patverb
    Contributor

    lvde0 Do you have an example repro dockerfile/project that I could see? We should scaffold the directory.build.props and other files automatically now so the restore should be cached unless we missed some files.

    You are also free to remove the dotnet build and dotnet restore and just have the dotnet publish step. They are just added to try to reduce build times using image caching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Needs more infoApply this label for the bugs where a some new information is needed from the customer.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions