Fastest way to build .NET Core App docker image

There are many ways and tools to build docker images. However, when working with .NET Core building images might be a kind of confusing. In this post I will desribe an intersting approach. I'm not saying that this is the best posssible way. But it works very well for me for very log time now.
1796_dockerimagebuild1

To add Docker support, select the project and do following. You will be asked to chose between Linux and Windows.

1799_dockerimagebuild2
This will create a new project in the existing solution. In this post we are not interseted on newly created project. All we need is dockerfile, which this project template will create inside of your existing project.

FROM mcr.microsoft.com/dotnet/core/runtime:3.0-stretch-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-stretch AS build
WORKDIR /src
COPY ["ConsoleApp1/ConsoleApp1.csproj", "ConsoleApp1/"]
RUN dotnet restore "ConsoleApp1/ConsoleApp1.csproj"
COPY . .
WORKDIR "/src/ConsoleApp1"
RUN dotnet build "ConsoleApp1.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "ConsoleApp1.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]

To build image from it, navigate to folder which contains the solution file of your application and execute following:

docker build --rm -f "ConsoleApp1\Dockerfile" -t consoleapp1:latest .

comments powered by Disqus