Bob5421
Bob5421

Reputation: 9093

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

Upvotes: 4

Views: 4833

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239320

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 . .

Upvotes: 4

Michael Hobbs
Michael Hobbs

Reputation: 1693

This is a normal amount of time, based on my experiences using the free service. I am assuming it would be faster with the non-free account options. Another option would be to set up a local build server/vm. You can quite easily run a gitlab container along with gitlab-runner containers. From there you setup up a gitlab ci file and do all this locally and then push the images to Docker Hub.

Upvotes: 2

Related Questions