DotNet Developping a Blazor App running in a docker container

Overview

Using DockerFile

Create an empty Text file named “dockerfile”.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

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

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

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

To build the project and create the docker image :

docker build -t blazor-myproject .

The image will expect the Blazor app to run on port 8080. To link to the current machine :

docker run --name myproject -p 8080:8080 -d blazor-myproject

to run the app, open your favorite browser with this address : http://localhost:8080/.

To stop and cleanup:

docker container stop myproject
docker container prune

Remark : To reduce the size of the container, I’ve used the following image as base : docker pull mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled Size went down from 210 Mb to 112 Mb.

References