ASP.NET Core Dockerfile无法成为原始路径相对

发布于 2025-01-31 06:22:22 字数 3064 浏览 1 评论 0原文

我们遇到一个非常奇怪的问题,可以为我们的一个API创建一个docker映像,该API在一个由Web API项目引用的单独项目中包含GRPC原型。

这里最明显的区别是,原始文件位于一个单独的项目中。我们还有另一个示例,其中proto文件位于主Web API项目中,并且Dockerfile对该结构运行良好。

此解决方案具有多个项目,并且所有项目都很好,除了一个具有GRPC的项目。 。这是重要的结构:

”在此处输入图像描述“

我们有一个名为“ Eventsengine”的文件夹,其中包含REST API项目,称为“ htlabs.bloxs.blox.eventsapi”,其中包含dockerfile。该项目引用了另一个项目“ htlabs.events.services”,其中包含一个称为库存/proto的子文件夹,其中包含原始文件。

HtECommerce.sln
  EventsEngine
    HtLabs.Blox.EventsApi
      Dockerfile
    HtLabs.Events.Services
      Inventory
        Protos
          SimpleInventory.proto

htlabs.events的csproj。服务表明protos像这样正确设置了:

<ItemGroup>
  <Protobuf Include="Inventory\Protos\SimpleInventory.proto" GrpcServices="Server,Client" />
  <Protobuf Include="Events\Protos\Events.proto" GrpcServices="Server" />
  <Protobuf Include="Events\Protos\Tenants.proto" GrpcServices="Server" />
</ItemGroup>

dockerfile非常复杂,因为涉及多个项目,但主要内容如下:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["*.sln", "./"]
COPY ["EventsEngine/HtLabs.Blox.EventsApi/HtLabs.Blox.EventsApi.csproj", "HtLabs.Blox.EventsApi/"]
COPY ["EventsEngine/HtLabs.Events.Services/HtLabs.Events.Services.csproj", "HtLabs.Events.Services/"]
COPY ["EventsEngine/HtLabs.Blox.EventsApi/nuget.config", ""]

RUN dotnet restore "HtLabs.Blox.EventsApi/HtLabs.Blox.EventsApi.csproj"

COPY . .
WORKDIR "/src/HtLabs.Blox.EventsApi"
RUN dotnet build "HtLabs.Blox.EventsApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "HtLabs.Blox.EventsApi.csproj" -c Release -o /app/publish

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

我们也知道需要从根部运行dockerfiles (即解决方案所在的文件夹)。对于没有GRPC的其他微服务,这很好。

但是,当我们添加GRPC原型的那一刻,我们会遇到以下错误:

Could not make proto path relative : error : Inventory/Protos/SimpleInventory.proto: No such file or directory [/src/HtLabs.Events.Services/HtLabs.Events.Services.csproj]

一些问题:

  • 我们需要还原每个引用项目吗?如在其中,我需要在Dockerfile中包括每个项目吗?
RUN dotnet restore "HtLabs.Events.Services/HtLabs.Events.Services.csproj"

真的很感谢这里的任何帮助。谢谢你!

We are running into a pretty odd issue in creating a docker image for one of our APIs that contain gRPC protos in a separate project that is referenced by as Web API project.

The most notable difference here is that the proto files reside in a separate project. We have another example where the proto file resides in the the main Web API project and the Dockerfile runs just fine for that structure.

This solution has multiple projects, and all of them work well, except the one that has the gRPC. Here is the structure that matters:

enter image description here

We have a folder called "EventsEngine" that contains the REST API project called "HtLabs.Blox.EventsApi" which contains the Dockerfile. This project references another project "HtLabs.Events.Services" which contains a sub-folder called Inventory/Proto which contains the proto file.

HtECommerce.sln
  EventsEngine
    HtLabs.Blox.EventsApi
      Dockerfile
    HtLabs.Events.Services
      Inventory
        Protos
          SimpleInventory.proto

The csproj for the HtLabs.Events.Services shows the protos correctly setup like so:

<ItemGroup>
  <Protobuf Include="Inventory\Protos\SimpleInventory.proto" GrpcServices="Server,Client" />
  <Protobuf Include="Events\Protos\Events.proto" GrpcServices="Server" />
  <Protobuf Include="Events\Protos\Tenants.proto" GrpcServices="Server" />
</ItemGroup>

The Dockerfile is very complex, as there are multiple projects involved, but the main stuff is as follows:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["*.sln", "./"]
COPY ["EventsEngine/HtLabs.Blox.EventsApi/HtLabs.Blox.EventsApi.csproj", "HtLabs.Blox.EventsApi/"]
COPY ["EventsEngine/HtLabs.Events.Services/HtLabs.Events.Services.csproj", "HtLabs.Events.Services/"]
COPY ["EventsEngine/HtLabs.Blox.EventsApi/nuget.config", ""]

RUN dotnet restore "HtLabs.Blox.EventsApi/HtLabs.Blox.EventsApi.csproj"

COPY . .
WORKDIR "/src/HtLabs.Blox.EventsApi"
RUN dotnet build "HtLabs.Blox.EventsApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "HtLabs.Blox.EventsApi.csproj" -c Release -o /app/publish

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

We also understand that Dockerfiles need to be run from the root (i.e. the folder where the solution resides). This has worked very well for other microservices that do not have gRPC.

However, the moment we add a gRPC proto, we run into the following error:

Could not make proto path relative : error : Inventory/Protos/SimpleInventory.proto: No such file or directory [/src/HtLabs.Events.Services/HtLabs.Events.Services.csproj]

A few questions:

  • Do we need to restore each referenced project? As in, do I need to include each project in the Dockerfile?
RUN dotnet restore "HtLabs.Events.Services/HtLabs.Events.Services.csproj"
  • I have read elsewhere that this might have to do something with the way the Dockerfile is created. The marked answer shows a Dockerfile but doesn't really state exactly what was changed on the Dockerfile to make this work. Any thoughts?

  • I have also read something about setting ProtoRoot but couldn't find any concrete/correct examples of doing so.

Would really appreciate any assistance here. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文