0

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 mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
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 mcr.microsoft.com/dotnet/core/sdk:2.2 as build
WORKDIR /src
COPY . .
RUN dotnet restore "myapp.csproj"
RUN dotnet build "myapp.csproj" -c Release -o /app

Is there any hidden sense in 2 times copy?

Pavlo K
  • 837
  • 9
  • 15
  • 1
    If installing the dependencies is expensive or at least takes time, `COPY`ing only the file that lists them first means Docker layer caching can skip the installation step if the `OktaMvcLogin.csproj` file hasn't changed. [Docker how to run pip requirements.txt only if there was a change?](https://stackoverflow.com/questions/34398632/docker-how-to-run-pip-requirements-txt-only-if-there-was-a-change/34399661#34399661) is a similar setup but in Python. – David Maze Apr 04 '21 at 13:41
  • @David Maze. Thank you for explanation and useful link. If you post this as an answer, I'd be happy to 'accept it' – Pavlo K Apr 04 '21 at 18:28

0 Answers0