I'm trying to build efficient dockerfile
, so package restore will be triggered only when package removed/added/updated. This is what I tried (based on the official sample):
FROM microsoft/dotnet:2.0-sdk-stretch AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY ./src/myapp.csproj ./
RUN dotnet restore && \
dotnet add package ILLink.Tasks -v 0.1.4-preview-981901 -s https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
# Copy everything else and build
COPY ./src ./
RUN dotnet publish -c Release -o out -r linux-x64
# Build runtime image
FROM microsoft/dotnet:2.0-runtime-deps
RUN useradd -d /home/dotnet -ms /bin/bash dotnet
USER dotnet
WORKDIR /home/dotnet/app
ENV ASPNETCORE_URLS=http://+:9999
COPY --from=build-env /app/out ./
ENTRYPOINT ["./myapp"]
I'm copying the csprog
, running dotnet restore
, and then copy the rest of files, and build. This should have the expected behavior - restoring packages only when needed. But this is not what happened - from some reason (couldn't find anything about it on the documentation) dotnet publish
trigger restore, although the packages are already cached:
Sending build context to Docker daemon 131.3MB
Step 1/13 : FROM microsoft/dotnet:2.0-sdk-stretch AS build-env
---> 17fc4fa98e0b
Step 2/13 : WORKDIR /app
---> Using cache
---> 9b13d975844b
Step 3/13 : COPY ./src/myapp.csproj ./
---> Using cache
---> fed39192abce
Step 4/13 : RUN dotnet restore && dotnet add package ILLink.Tasks -v 0.1.4-preview-981901 -s https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
---> Using cache
---> efcdaf201661
Step 5/13 : COPY ./src ./
---> a50fd8fa6106
Removing intermediate container 9eadb5543dbe
Step 6/13 : RUN dotnet publish -c Release -o out -r linux-x64
---> Running in 3bf17790a376
Microsoft (R) Build Engine version 15.7.177.53362 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restoring packages for /app/myapp.csproj...
Restore completed in 1.4 sec for /app/Hamuste.csproj.
Installing <redacted>
Installing <redacted>
Installing <redacted>
What am I missing? How can I improve this docker file?