无法在Docker中正确映射多个.net应用程序的端口



我正在尝试为两个。net应用程序创建Docker容器并正确映射它们的端口。但是,我在当前设置中遇到了一些问题。

我的Dockerfile和dockercompose。格式如下:

Dockerfile (DemoAppTwo):

#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:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["DemoAppTwo/DemoAppTwo.csproj", "DemoAppTwo/"]
RUN dotnet restore "DemoAppTwo/DemoAppTwo.csproj"
COPY . .
WORKDIR "/src/DemoAppTwo"
RUN dotnet build "DemoAppTwo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DemoAppTwo.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoAppTwo.dll"]

Dockerfile (DemoApp):

#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:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["DemoApp/DemoApp.csproj", "DemoApp/"]
RUN dotnet restore "DemoApp/DemoApp.csproj"
COPY . .
WORKDIR "/src/DemoApp"
RUN dotnet build "DemoApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DemoApp.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoApp.dll"]

docker-compose.yml:

version: '3.4'
services:
demoapptwo:
image: ${DOCKER_REGISTRY-}demoapptwo
build:
context: .
dockerfile: DemoAppTwo/Dockerfile
ports:
- "5001:80"
- "5002:443"
demoapp:
image: ${DOCKER_REGISTRY-}demoapp
build:
context: .
dockerfile: DemoApp/Dockerfile
ports:
- "5003:80"
- "5004:443"

尽管进行了配置,我还是无法访问指定端口(5001、5002、5003和5004)上的应用程序。有人可以帮助我确定问题并指导我如何正确映射两个应用程序的端口吗?输入图片描述

您是否检查过您的。net应用程序实际上正在运行并使用指定的端口(80,443)?dockerfile中的EXPOSE指令实际上并不影响。net应用程序。如果你没有在appsettings或startup中硬编码应用程序的端口,如果我没弄错的话,应用程序实际上可能会暴露在容器内的5000(http)和5001(https)。
你可以连接到容器并检查。net应用程序是否正在运行以及在哪些端口上运行:

  1. 连接到容器,例如docker exec -it <container name> bash
  2. Install net-toolsapt-get install -y net-tools
  3. 检查netstat -tulp运行情况
  4. 你应该看到类似这样的内容(在这个例子中是postgreSql服务)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 [::]:5432               [::]:*                  LISTEN      -

在Dockerfile中指定。net应用程序端口的一种方法是使用环境变量ENV ASPNETCORE_URLS,例如,在你的Dockerfiles中,而不是这样:

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

试试这个:

FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:80;https://+:443
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DemoAppTwo.dll"]

相关内容

  • 没有找到相关文章

最新更新