无法在Docker中为多个.NET应用程序正确Map端口

nx7onnlm  于 2023-04-11  发布在  Docker
关注(0)|答案(1)|浏览(117)

我正在尝试为两个.NET应用程序创建Docker容器并正确Map它们的端口。但是,我当前的设置遇到了问题。
我的Dockerfile和docker-compose.yml如下:
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)上的应用程序。有人可以帮助我确定问题并指导我如何正确Map两个应用程序的端口吗?enter image description here

iklwldmw

iklwldmw1#

你有没有检查过你的.NET应用程序实际上正在运行并使用指定的端口(80,443)?dockerfile中的EXPOSE指令实际上不会影响.NET应用程序。如果你没有在appsettings或startup中硬编码应用程序的端口,如果我没有弄错的话,应用程序实际上可能会暴露在容器中的5000(http)和5001(https)。
您可以连接到容器并检查.NET应用程序是否实际运行以及在哪些端口上运行:
1.连接到一个容器,例如docker exec -it <container name> bash
1.安装net-tools apt-get install -y net-tools
1.检查正在运行的netstat -tulp
1.您应该会看到类似这样的内容(本例中的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"]

相关问题