Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Blazor in Docker

I've created my first Blazor test using Visual Studio 2019 (Preview). The repo is here. Everything works as expected when I hit the IISExpress button in VS.

However, I'd like to deploy my app to an Ubuntu/Dokku server. As a result, I've been trying to figure out how I might setup my server using Docker. Here's what I have so far in my Dockerfile:

# Latest .NET Core from https://hub.docker.com/_/microsoft-dotnet-core-sdk/ (not the nightly one)
FROM mcr.microsoft.com/dotnet/core/sdk:3.0.100-preview6-disco AS build-env
WORKDIR /app
# Copy everything and build
COPY . ./
RUN dotnet restore "./HollyTest.Server/HollyTest.Server.csproj"
RUN dotnet publish "./HollyTest.Server/HollyTest.Server.csproj" -c Release -o out

# Latest ASP.NET Core from https://hub.docker.com/_/microsoft-dotnet-core-aspnet/ (not the nightly one)
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0.0-preview6-disco
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "HollyTest.Server.dll"]

The app compiles and the server runs, but everytime I hit the server I get a 404. So I've run the above commands on my local machine:

cd app
dotnet restore .\HollyTest.Server\HollyTest.Server.csproj
dotnet publish "./HollyTest.Server/HollyTest.Server.csproj" -c Release -o out
cd out
dotnet .\HollyTest.Server.dll

Then when I run curl localhost:5000 I see the following:

Hosting environment: Production
Content root path: C:\Users\hmurphy\Documents\GitHub\app\out
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 223.05540000000002ms 404
Application is shutting down...

In the log generated by Visual Studio, it looks healthier:

Hosting environment: Development
Content root path: C:\Users\hmurphy\Documents\GitHub\HollyTest\HollyTest.Server
Now listening on: http://localhost:50873
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://localhost:50873/
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[6]
      The file /index.html was not modified
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Fallback {*path:nonfile}'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 326.5401ms 304 text/html
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
.
.
.

I'm assuming that I'm missing some sort of configuration/missing a step. Is it do with the "content root path"? In the ./out folder, there is no HollyTest.Server folder.

I don't have much experience with ASP.NET Core (or modern web development in .NET). Can anybody help me and point me in the right direction?

like image 823
Mitkins Avatar asked Jul 02 '19 07:07

Mitkins


1 Answers

There well-known issue in Balzor on .NET Core Preview 6 (https://github.com/aspnet/AspNetCore/issues/11636) and (https://github.com/aspnet/AspNetCore/issues/11185)

Either you wait for Preview 7, or add to your Dockerfile

# After publish
COPY out/wwwroot/_content/hollytestclient out/wwwroot/

then run docker run -p 8081:5000 -it holly_test:latest

like image 90
codevision Avatar answered Nov 20 '22 13:11

codevision