Andreas Zita
Andreas Zita

Reputation: 7560

How to use dotnet restore properly in Dockerfile

When adding docker support to a ASP.NET Core project VS (15.9.2) will add a default Dockerfile that does restore, build and publish. But instead of just copying all files into the Docker build container it first copies just the proj-files, does the restore and then copies the rest before it builds. I was wondering why this is done like this? In what way is that different from just copying all files directly and then doing the restore?

The problem with this approach is that all proj-files in the solution will need to be copied separately and if the project is really big with projects being added and removed from time to time it's a bit hard to keep the Dockerfile in synch with this. I just like to know why this is done like this and if it would be just as ok to copy everything instead?

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Temp2/Temp2.csproj", "Temp2/"]
COPY ["Temp3/Temp3.csproj", "Temp3/"]
RUN dotnet restore "Temp2/Temp2.csproj"
COPY . .
WORKDIR "/src/Temp2"
RUN dotnet build "Temp2.csproj" -c Release -o /app

or

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY . .
RUN dotnet restore "Temp2/Temp2.csproj"
WORKDIR "/src/Temp2"
RUN dotnet build "Temp2.csproj" -c Release -o /app

Upvotes: 15

Views: 13781

Answers (1)

Kirk Larkin
Kirk Larkin

Reputation: 93063

When Docker builds an image, it maintains a build cache:

When building an image, Docker steps through the instructions in your Dockerfile, executing each in the order specified. As each instruction is examined, Docker looks for an existing image in its cache that it can reuse, rather than creating a new (duplicate) image.

Importantly, the ADD and COPY instructions get special treatment:

For the ADD and COPY instructions, the contents of the file(s) in the image are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. During the cache lookup, the checksum is compared against the checksum in the existing images. If anything has changed in the file(s), such as the contents and metadata, then the cache is invalidated.

When building a .NET Core solution, we can be sure that after running dotnet restore, the result of running dotnet restore again will only change if the .csproj file has changed (e.g. a new package is added or a version is changed).

By copying the .csproj files into the image separately, we can take advantage of Docker's build cache, which means that so long as the .csproj file hasn't changed, the dotnet restore step will not be re-executed unnecessarily each and every time the image gets rebuilt.

Upvotes: 17

Related Questions