为什么端口在Docker中不转发?

6jjcrrmo  于 11个月前  发布在  Docker
关注(0)|答案(1)|浏览(124)

我的DockerFile:

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
#USER app
WORKDIR /app
EXPOSE 80/tcp

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Services/Catalog/Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
RUN dotnet restore "./Services/Catalog/Catalog.API/./Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "./Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Catalog.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

字符串
我的docker-compose文件

version: '3.4'

services:

  catalogdb:
    container_name: cataloggdb
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db 

  catalog.api:
    container_name: catalog.api
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "DatabaseSettings:ConnectionString=mongodb://catalogdb:27017"
    depends_on:
      - catalogdb
    ports:
      - "8000:80"


我执行

docker-compose -f .\docker-compose.yml -f .\docker-compose.override.yml up -d

✔ Network src_default    Created                                                                                  0.0s
 ✔ Container cataloggdb   Started                                                                                  0.1s
 ✔ Container catalog.api  Started


x1c 0d1x的数据
当我转到http://localhost:27017/时,它工作:



当我转到http://localhost:8000/swagger/index.html时,它不工作:


f45qwnt8

f45qwnt81#

EXPOSE实际上并没有设置端口,它基本上是文档:
EXPOSE指令实际上并不发布端口,它的作用是作为构建镜像的人和运行容器的人之间的一种文档,关于哪些端口要发布。
由于.NET 8在ASP.NET Core镜像中默认端口是8080-参见Default ASP.NET Core port changed from 80 to 8080
或者更改Map以反映:

ports:
  - "8000:8080"

字符串
或者使用ASPNETCORE_HTTP_PORTS环境变量将端口设置为80

environment:
   - ASPNETCORE_HTTP_PORTS=80


FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
#USER app
WORKDIR /app
ENV ASPNETCORE_HTTP_PORTS=80
EXPOSE 80/tcp

相关问题