dockercontinuous-integrationcontinuous-deploymentasp.net-core-mvc-2.0

docker hub very slow


I have created a basic asp.net core MVC project with this command:

dotnet new mvc --name "myproject"

I have created this very basic Dockerfile which builds a runtime image:

FROM  mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY . .
RUN dotnet restore "./myproject.csproj"
RUN dotnet build "myproject.csproj" -c Release -o /app
RUN dotnet publish "myproject.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "myproject.dll"]

I have setup a github account and a docker hub account in order to build the image on each commit/push.

The building takes about 6 minutes. This is very slow. I am wondering the result on a real bigger project.

What can i do in order to speed-up this compilation ?

My goal is to put on production the image (CI pipeline). If i fix a bug, i can't wait 10 minutes. This is paradoxal because i used to work with an old school method: ftp transfert of binary files and it takes ... 30 seconds ! I am wrong somewhere in this goal ?

Thanks


Solution

  • Well, one problem is the COPY . . before dotnet restore. Restoring the NuGet packages can take quite a bit of time. Docker caches each image layer (basically each command) and will use the cache instead of running that command again. However, if any layer before it changes, then all subsequent layers must be rebuilt. Since with COPY . . any change to any file will trigger a rebuild of that layer, then you're forced to rebuild the dotnet restore layer each time as well, which is highly inefficient.

    The proper way to do this is to copy just the project file, restore, then copy all the rest of the files:

    COPY myproject.csproj .
    RUN dotnet restore "./myproject.csproj"
    COPY . .