Static Blazor in NGINX

My first blog post on Blazor talked about setting up the site and sharing data models. My second blog post on Blazor talked about testing the site with Jest. This blog post is putting Blazor into a Docker image.

Docker

This Dockerfile below is a multistage build that builds the Blazor app using the .NET Core SDK as the base image. The output of the build is static assets, which are then copied over to the final build for Docker.

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

COPY . ./
WORKDIR /app/Blazor.Client
RUN dotnet publish -c Release

FROM nginx:alpine

WORKDIR /var/www/web
COPY --from=build-env /app/Blazor.Client/bin/Release/netstandard2.0/publish/Blazor.Client/dist .
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

Be sure to have the dockerignore file too.

bin/
obj/
.vs/
node_modules/
!*.dll
!obj/Docker/publish/*
!obj/Docker/empty/

NGINX config

The example NGINX config on the Blazor site is here. I've extended it below to the bare minimum needed for Blazor. The big things being to add the mime types.

events { }
http {
    include mime.types;
    types {
        application/wasm wasm;
    }
    server {
        listen 80;
        index index.html;

        location / {
            root /var/www/web;
            try_files $uri $uri/ /index.html =404;
        }
    }
}

Build and run

> docker build -t blazor .
> docker run --rm -p 80:80 blazor

Summary

My code is here.