简体   繁体   English

具有 docker 支持的 Blazor WebAssembly 应用程序 (linux)

[英]Blazor WebAssembly App with docker support (linux)

Today I have a question which is one of those questions, that you always did want to know but you were always afraid to ask;) But this question is also correlated with BlazorWebAssembly app docker support.今天我有一个问题,这是其中一个问题,你一直想知道但你总是害怕问;) 但是这个问题也与 BlazorWebAssembly 应用程序 docker 支持相关。 So maybe I will describe what I did.所以也许我会描述我做了什么。

SO I did want to play a little bit with cutting edge blazor and of course .NET 5.0 (my last "cutting edge technology" was ASP.NET MVC - so yeah, it have been a while :) )所以我确实想玩一点尖端 blazor,当然还有 .NET 5.0(我最后一个“尖端技术”是 ASP.NET MVC - 所以是的,已经有一段时间了:))

In the beginning i didn't quite know what those option correlated with Blazor was for.一开始我不太清楚那些与 Blazor 相关的选项是做什么用的。 So i've chosen:所以我选择了:

在此处输入图片说明

You can spot, that the option "Enable Docker Support" is greyed out.您可以发现,“启用 Docker 支持”选项是灰色的。 So it was like "docker support problem preview" :)所以就像“docker支持问题预览”:)

I didn't know what option: "ASP.NET Core hosted" stand for, so I didn't use it (and it was a mistake) Once you check this option it will create one server app, one client (Blazor) one and one shared (which is for model purposes between them)我不知道什么选项:“ASP.NET Core 托管”代表,所以我没有使用它(这是一个错误)一旦您选中此选项,它将创建一个服务器应用程序,一个客户端(Blazor)一个和一个共享(这是为了他们之间的模型目的)

I didn't know that, so I did have to add on my own WebApi project and some project for the model :)我不知道,所以我必须添加我自己的 WebApi 项目和模型的一些项目:)

Now, the next step was to add docker support (which surprisingly is possible)现在,下一步是添加 docker 支持(令人惊讶的是这是可能的)

在此处输入图片说明

And this created a docker file:这创建了一个 docker 文件:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["BlazorApp/BlazorApp.csproj", "BlazorApp/"]
COPY ["../SearchMovies.Model/SearchMovies.Model.csproj", "../SearchMovies.Model/"]
RUN dotnet restore "BlazorApp/BlazorApp.csproj"
COPY . .
WORKDIR "/src/BlazorApp"
RUN dotnet build "BlazorApp.csproj" -c Release -o /app/build

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

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

Starting such app will end with something like that:启动这样的应用程序将以这样的方式结束:

docker exec -i -e ASPNETCORE_HTTPS_PORT="32776" -w "/app" 28d444a42ceefdf124b29f9f542f423aa931e521d077bbfeb19e5da7644a5e25 sh -c ""dotnet" --additionalProbingPath /root/.nuget/fallbackpackages2 --additionalProbingPath /root/.nuget/fallbackpackages --additionalProbingPath /root/.nuget/fallbackpackages3  \"/app/bin/Debug/net5.0/BlazorApp.dll\" | tee /dev/console"
Cannot use file stream for [/app/bin/Debug/net5.0/BlazorApp.deps.json]: No such file or directory
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/bin/Debug/net5.0/'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because '/app/bin/Debug/net5.0/BlazorApp.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the '/app/bin/Debug/net5.0/BlazorApp.runtimeconfig.json' file and specify the appropriate framework.

I've already googled it, and the answer was: yeah, it is normal behaviour.我已经用谷歌搜索过了,答案是:是的,这是正常行为。 Blazor is just a piece of static files (whicha are only "smartly" produced) So that is why it doesn't work. Blazor 只是一块静态文件(只是“智能”生成的)所以这就是它不起作用的原因。 Tbh, I hear them, but still... Why o why? Tbh,我听到他们,但仍然......为什么?为什么?

I've googled it a little bit.我用谷歌搜索了一下。 I found some nice article about Blazor Webassembly here:我在这里找到了一些关于 Blazor Webassembly 的好文章:

https://medium.com/@waelkdouh/hosting-containerized-client-side-blazor-applications-on-azure-e090af738619 https://medium.com/@waelkdouh/hosting-containerized-client-side-blazor-applications-on-azure-e090af738619

Some fun fact at the begining (the quote from the article)开头的一些有趣事实(文章中的引用)

Note: I imagine this checkbox will be enabled sometime in the future注意:我想这个复选框将在未来某个时候启用

So guys, just to calm you.所以伙计们,只是为了让你平静下来。 We are definively not in the futrue :)我们绝对不在未来:)

So i've changed the docker file.所以我改变了docker文件。 And if i remember correctly - runing it "manually" as in the article by using "docker build" and "docker run" it started successfuly.如果我没记错的话 - 像文章中一样使用“docker build”和“docker run”“手动”运行它,它成功启动。

But when trying to hit "Debug" (or start without debuging) the error was the same as previously.但是当尝试点击“调试”(或在不调试的情况下启动)时,错误与以前相同。 Here is the docker file proposed there (i've only changed the version to 5.0 and changed publish to run only on the project):这是那里提出的 docker 文件(我只将版本更改为 5.0 并将发布更改为仅在项目上运行):

FROM mcr.microsoft.com/dotnet/core/sdk:5.0-buster-slim AS build-env
WORKDIR /app
COPY . ./
RUN dotnet publish "BlazorApp/BlazorApp.csproj" -c Release -o output

FROM nginx:alpine
WORKDIR /var/www/web
COPY --from=build-env /app/output/wwwroot .
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

So two attempts.所以两次尝试。 I've checked also another cutting edge orchestration thingy called tye :我还检查了另一个名为tye 的尖端编排东西:

https://devblogs.microsoft.com/aspnet/introducing-project-tye/ https://devblogs.microsoft.com/aspnet/introducing-project-tye/

But it cannot handle the Blazor as well.但它也不能处理 Blazor。

I've even quickly created blazor app with ASP.NET Core hosted option.我什至快速创建了带有 ASP.NET Core 托管选项的 blazor 应用程序。 After tye init , tye build and tye run there are no bindings for the client project:tye inittye buildtye run之后,客户端项目没有绑定:

在此处输入图片说明

So maybe someone have some idea, what can i try?所以也许有人有一些想法,我可以尝试什么?

And the question mentioned at the very begining was regarding orchestration i suppose.我想一开始提到的问题是关于编排的。 Let's assume i have four projects in solution - for example, Blazor and three webapi`s.假设我在解决方案中有四个项目 - 例如 Blazor 和三个 webapi。 How to run everything so that it will automagically bind the endpoints.如何运行一切以便它自动绑定端点。 Because normally i have to add the url for endpoints like: "https://localhost:5123/api/SomeService"因为通常我必须为端点添加网址,例如:“https://localhost:5123/api/SomeService”

And docker at least when starting can assign different port numbers for those API's Changing in the code manually seems like the dummy thing to do.并且 docker 至少在启动时可以为这些 API 分配不同的端口号手动更改代码似乎是一件愚蠢的事情。

What is the best way to tackle such cases - where there is a lot o communications between endpoints and i want to start all of them and they will be automatically "connected" (meaning, they will know on which port the "other" service is)?解决这种情况的最佳方法是什么 - 端点之间有很多 o 通信,我想启动所有这些,它们将自动“连接”(意思是,它们将知道“其他”服务在哪个端口上) )?

Thanks in advance for anwsers on those two questions预先感谢您对这两个问题的回答

You could use Docker Compose for several apps.您可以将Docker Compose用于多个应用程序。 YAML file docker-compose.yml will refer to Dockerfile for each app: YAML文件docker-compose.yml将引用每个应用程序的Dockerfile

version: '3.4'

services:
  webfrontend:
    image: ${DOCKER_REGISTRY-}webfrontend
    build:
      context: .
      dockerfile: WebFrontEnd/Dockerfile

  mywebapi:
    image: ${DOCKER_REGISTRY-}mywebapi
    build:
      context: .
      dockerfile: MyWebAPI/Dockerfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM