【问题标题】:Docker container communication in .NET Core.NET Core 中的 Docker 容器通信
【发布时间】:2020-10-16 12:28:24
【问题描述】:

我有两个 .NET Core3 API。一个充当 API,另一个充当 BFF。 Docker文件和docker-compose的API在5000中暴露如下:

version: "3.8"
services:
  api:
    container_name: api
    build: 
      context: ./TSTMaxAPI
    image: tstmaxapi:latest
    ports:
      - "5000:443"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+80
      - ASPNETCORE_Kestrel__Certificates__Default__Password=Admin.123
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
    volumes:
      - ~/.aspnet/https:/https:ro

Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 as base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /src
COPY ["TSTMaxAPI.csproj", "./"]
RUN dotnet restore "./TSTMaxAPI.csproj"
COPY . .
RUN dotnet build "TSTMaxAPI.csproj" -c Release -o /app

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

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

当我运行 docker-compose 时,我可以访问 https://localhost:5000/weatherforecast 我可以访问 https://localhost:5000/health 好像还可以

现在是暴露在 4000 的 BFF

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 as base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build
WORKDIR /src
COPY ["TSTMaxBFF.csproj", "./"]
RUN dotnet restore "./TSTMaxBFF.csproj"
COPY . .
RUN dotnet build "TSTMaxBFF.csproj" -c Release -o /app

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

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

还有 docker-compose

version: "3.8"
networks:
  tst_network:
services:
  bff:
    container_name: bff
    build: 
      context: ./TSTMaxBff
    image: tstmaxbff:latest
    restart: on-failure 
    ports:
      - 4000:443
    environment:
      - API_HOST=https://api:5000
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+80
      - ASPNETCORE_Kestrel__Certificates__Default__Password=Admin.123
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
    volumes:
      - ~/.aspnet/https:/https:ro
    networks:
      - tst_network
    depends_on:
      - api

  api:
    container_name: api
    image: tstmaxapi:latest   
    restart: on-failure  
    ports:
      - 5000:443
    networks:
      - tst_network

现在当我在这里运行 docker-compose 时,看起来 BFF 和 API 容器都在旋转。

我可以访问 https://localhost:4000/health 然而, 我无法访问 https://localhost:5000/weatherforecast 我无法访问 https://localhost:5000/health 和 https://localhost:4000/weatherforecast,调用API,相关代码如下

private static string API_URL = Environment.GetEnvironmentVariable("API_HOST");
private static string API_ENDPOINT = API_URL + "/weatherforecast";
var response = await _client.GetStringAsync(API_ENDPOINT);

并得到以下错误

SocketException: Connection refused
System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

HttpRequestException: Connection refused
System.Net.Http.ConnectHelper.ConnectAsync(string host, int port, CancellationToken cancellationToken)

docker ps 输出

我需要这 2 个容器在开发模式和生产模式下相互通信。任何帮助表示赞赏。谢谢

【问题讨论】:

  • 我不确定tst_network 在这里做了什么,但您可以尝试将主机从localhost 调用另一个服务首先更改为host.docker.internal

标签: c# docker asp.net-core docker-compose azure-devops


【解决方案1】:

首先,服务到服务的通信使用容器端口,而不是发布端口。

    environment:
      - API_HOST=https://api:5000

因为 API 容器端口是 443

    ports:
      - 5000:443

所以 API 主机应该是

    environment:
      - API_HOST=https://api:443

默认情况下,Docker 通过非联网的 UNIX 套接字运行并使用 HTTP 套接字进行通信。

为什么要在容器内启用 HTTP?这应该使用 Nginx 或其他负载均衡器来完成。

如果上述方法不起作用,那么您也可以尝试使用 HTTP

API_HOST=http://api:443

【讨论】:

  • 尝试了所有组合,仍然无法正常工作。另外,我也无法从浏览器中访问 localhost:5000。
  • 好的,在容器curl https://localhost 内尝试命令并检查容器是否响应此
  • 谢谢。修复。它缺少证书,因此可以访问 https。所以localhost5000 抛出错误。我确实尝试 curl 来获取此信息。通过提供适当的证书来修复它。
  • @RohithRaveendranPoyyeri 你可以add it as answeraccept it,让更多的会员从你的回答中看到有用的信息~
【解决方案2】:

修复了它,因为缺少证书。这是最终的 docker-compose 文件,以防有人发现它有帮助

version: "3.8"

services:
  bff:
    container_name: bff
    build: 
      context: ./Bff
    image: bff:latest
    restart: on-failure 
    ports:
      - 4000:443
    environment:
      - API_HOST=https://api
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+80
      - ASPNETCORE_Kestrel__Certificates__Default__Password=Admin.123
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
    volumes:
      - ~/.aspnet/https:/https:ro
    depends_on:
      - api

  api:
    container_name: api
    image: api:latest   
    restart: on-failure  
    ports:
      - 5000:443
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+80
      - ASPNETCORE_Kestrel__Certificates__Default__Password=Admin.123
      - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
    volumes:
      - ~/.aspnet/https:/https:ro

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多