I am creating a Docker container from this .NET Core application that has multiple projects inside the solution, and I wonder if it is better to COPY
each project file separately, or in a single line.
Visual Studio generates a Dockerfile that copies each project file on a new line, like this:
WORKDIR /src
COPY Dir1/Dir1.csproj Dir1/
COPY Dir2/Dir2.csproj Dir2/
COPY Dir3/Dir4/Dir4.csproj Dir3/Dir4/
RUN dotnet restore Dir1/Dir1.csproj
However, the Microsoft documentation for creating a .NET application with Docker shows as an "optimization" to use only a single COPY
statement, like so:
WORKDIR /src
COPY . .
RUN dotnet restore Dir1/Dir1.csproj
Which is said to, in my understanding of the article, create a larger image, but wouldn't have to COPY
all other projects if one project changes.
Interestingly, the Docker documentation mentions to
COPY
them individually, rather than all at once. This ensures that each step’s build cache is only invalidated (forcing the step to be re-run) if the specifically required files change.
Which basically contradicts what the Microsoft article says, about the COPY
step having to be rerun when one project changes.
I would like to know which option is better, and why, or for what purpose. Or perhaps I misunderstood some of the documentation, then please explain the difference to me.