简体   繁体   English

如何在 docker 容器中运行 .net 核心 grpc?

[英]How to run .net core grpc in docker container?

Hi I have created GRPC project using.Net 5. Below is my Docker file.嗨,我使用.Net 5 创建了 GRPC 项目。下面是我的 Docker 文件。

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 ./*.sln ./
COPY */*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
RUN dotnet restore
COPY . .
WORKDIR "/src/Geography"
RUN dotnet build "Geography.csproj" -c Release -o /app/build

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

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

I tried to run solution in visual studio using Docker and I see container up and running.我尝试使用 Docker 在 Visual Studio 中运行解决方案,我看到容器启动并运行。 I see below ports exposed.我看到下面暴露的端口。

 0.0.0.0:32775->80/tcp, 0.0.0.0:32774->443/tcp

I created client application and tried to hit grpc end points as below.我创建了客户端应用程序并尝试点击如下 grpc 端点。

using var channel = GrpcChannel.ForAddress("http://localhost:32774");
var client = new Geographys.GeographysClient(channel);

Here I am getting below error在这里我得到以下错误

Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.. SocketException: An existing connection was forcibly closed by the remote host.", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.. ---> Status(StatusCode="Internal", Detail="Error started gRPC call. HttpRequestException: 发送请求时出错。IOException: 无法从传输连接中读取数据: 现有连接被远程主机强行关闭.. SocketException : 现有连接被远程主机强行关闭。", DebugException="System.Net.Http.HttpRequestException: 发送请求时出错。---> System.IO.IOException: 无法从传输中读取数据连接:现有连接被远程主机强行关闭.. --->

can someone help me to understand the root cause of this issue?有人可以帮助我了解这个问题的根本原因吗? Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you谢谢

Are you confident that your .NET code is exposing a non-TLS HTTP endpoint on container port 80 and a TLS HTTP endpoint on 443 ?您确定您的 .NET 代码在容器端口80上暴露了非 TLS HTTP 端点,在443上暴露了 TLS HTTP 端点吗?

I suspect it isn't and this would explain why you're unable to access the service through the ports when they're bound to the host (on 32775 and 32774 respectively).我怀疑不是,这可以解释为什么当端口绑定到主机(分别在3277532774上)时,您无法通过端口访问服务。

NOTE Your client code tries to access http://localhost:32774 which possibly maps to the container's TLS endpoint ( :443 ) so you'd need to use https:// and configure a certificate.注意您的客户端代码尝试访问http://localhost:32774可能映射到容器的 TLS 端点 ( :443 ),因此您需要使用https://并配置证书。

If you're running Linux on the host, you can try telnet'ing to those ports to see whether you get a response:如果您在主机上运行 Linux,您可以尝试远程登录到这些端口以查看是否收到响应:

telnet localhost 32775
telnet localhost 32774

If you have gRPCurl (and IMO you should for any gRPC dev), you can try listing methods:如果您有gRPCurl (并且 IMO 应该适用于任何 gRPC 开发人员),您可以尝试列出方法:

grpcurl \
-plaintext \
-proto path/to/your.proto \
localhost:32775 \
list

grpcurl \
-plaintext \
-proto path/to/your.proto \
localhost:32774 \
list

NOTE I think that's the correct grpcurl for both TLS and non-TLS but writing without being able to test注意我认为这对于 TLS 和非 TLS 都是正确的grpcurl但无法测试

Adding the following code添加以下代码

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  }

in the appsettings.json file on the gRPC server side and calling 'http' port instead of 'https' on the gRPC client side should do the trick.appsettings.json服务器端的 appsettings.json 文件中,并在 gRPC 客户端调用“http”端口而不是“https”应该可以解决问题。 In your example that would be:在您的示例中,这将是:

using var channel = GrpcChannel.ForAddress("http://localhost:32775");

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

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