【发布时间】: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