I wanted to get a Blazor server application up and running on Ubuntu within a docker container (I’m running the whole thing on a Raspberry Pi 4 with Ubuntu Server).
The first stage for this post will simply be about creating a Dockerfile and creating a Blazor server application via the dotnet template.
We’re going to want the latest version of dotnet core, so let’s start by creating a very bare bones Dockerfile which will create an image based upon
mcr.microsoft.com/dotnet/core/sdk:3.1, it will also expose the standard HTTP port used in the Blazor server template, i.e. port 5000
Here’s the Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
ENV APP_HOME /home
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
EXPOSE 5000
CMD [ "bash" ]
To build this image run the following
docker rmi dotnet-env --force
docker build -t dotnet-env .
The first line is solely there to remove any existing image (which will be especially useful whilst developing the image). The second line will build the Dockerfile and name it dotnet-env.
Once built, let’s run the image to see all is good with it. So simply run
docker run -it --rm -p 5000:5000 -v /home/share:/home/share dotnet-env
In this example we’ll run docker in interactive mode and map ports using -p to map the host port 5000 to the exposed port in the image. We’ll also also created a volume link from the container to the host.
Once we’ve run the image up we should be placed into a BASH command prompt, now we can simply run
dotnet new blazorserver -o MyServer
To create the project MyServer, once created cd into the MyServer folder. Now run
dotnet run
A kestrel server should start up, and you might be able to access the server using http://server-ip-address. I say might, because you may well see an error at startup, saying something like
Unable to bind to http://localhost:5000 on the IPv6 loopback interface: ‘Cannot assign requested address’.
What you need to do is go into the Properties folder and open launchSettings.json, change the line
"applicationUrl": "https://localhost:5001;http://localhost:5000",
to
"applicationUrl": "http://0.0.0.0:5001;http://0.0.0.0:5000",
Next Step
This obvious next step to our docker build is to create a Docker image which contains our application and runs it when the container is started. We’re going to build and publish the using dotnet publish -c Release -o publish and then include the published files in our docker container, alternatively you might prefer to have the Dockerfile build and publish the project as part of its build process.
For now let’s just build our Blazor server application, then publish it to a folder.
We’re going to host the application in Kestrel, so before we go any further open the appsetting.json file from the publish folder and add the following
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5000"
}
}
},
Now we’ll make the changes to the Dockerfile to copy the published folder to the image and start up the Kestrel server when the image is run, here’s the Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
ENV APP_HOME /home
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY ./BlazorServer/BlazorServer/publish ${APP_HOME}
EXPOSE 5000
CMD [ "dotnet", "BlazorServer.dll" ]
Now you should be able to access your server using http://your_server_name:5000.