Build .net app in docker without extra copy step

  .net, build, docker

There are a lot of articles and samples that illustrates how to build .net app in docker. This one is example. And all of them (that I found) contains similar steps:

  1. Copy proj file to image
  2. restore dependencies
  3. copy rest of src files to image
  4. Build…

In mentioned example this is:

FROM AS build
COPY ["OktaMvcLogin.csproj", "./"]
RUN dotnet restore "./OktaMvcLogin.csproj"
COPY . .
RUN dotnet build "OktaMvcLogin.csproj" -c Release -o /ap

I’m wondering why to execute copy two times ? Why I can’t just copy everything and then run restore and build?

FROM as build
COPY . .
RUN dotnet restore "myapp.csproj"
RUN dotnet build "myapp.csproj" -c Release -o /app

Is there any hidden sense in 2 times copy?

Source: Docker Questions