Home > Mobile >  Why it is good to restore package in dockerfile
Why it is good to restore package in dockerfile

Time:04-30

I saw docker file like this:

FROM mcr.microsoft.com/dotnet/sdk:3.1
WORKDIR /src
COPY ["app/WebApplication.csproj", "app/"]
RUN dotnet restore "app/WebApplication.csproj"
COPY . .
WORKDIR "/src/app"
RUN dotnet build "WebApplication.csproj" -c Release -o /app/build

I don't understand why we need to do RUN dotnet restore "app/WebApplication.csproj" first, isn't dotnet build in the last instruction automatically trigger a dotnet restore automatically?

CodePudding user response:

It's a performance thing that makes your builds go faster. Sometimes.

Docker keeps a cache of build layers and checks if you've done what you're doing before. There's a layer for each statement in your Dockerfile. The Dockerfile you have makes the assumption that you don't change your .csproj file as often as the rest of your code.

Let's say you've done a build and docker has filled its cache. Then you change some code (but not the .csproj file) and build again. Now Docker can see that up until COPY . . everything is the same as last time you built, so it just takes the layers from the cache. So it doesn't actually do the dotnet restore and that can save a lot of time.

Of course if you change the .csproj file, it has to do the dotnet restore.

  • Related