You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose部署.NET 8 WebApi后无法访问问题求助

问题排查请求

我有一个基于.NET 8框架、搭配Postgres数据库的WebApi项目,计划用Docker Compose部署,但当前配置无法正常运行。执行docker-compose up后,API和Postgres都在Docker中启动了,WebApi的Docker日志显示Now listening on: http://[::]:8080,但我没法通过localhost:8080/swagger或localhost:5000/swagger访问Swagger页面,找不到配置问题,求下一步排查方向。

配置文件

docker-compose.yml

version: '3.4'

networks:
  latteofficeapi-dev:
    driver: bridge 

services:
  latteofficeapi:
    image: latteofficeapi:latest
    depends_on:
      - "postgres_image"
    build:
      context: .
      dockerfile: LatteOffice.API/Dockerfile
    ports:
      - "5000:5000"     
    environment:
      ConnectionStrings__LatteOfficeDb: "host=postgres_image;port=5432;database=latteofficedb;username=latteoffice_user;password=latteoffice_pass"
    networks:
      - latteofficeapi-dev  
  
  postgres_image:
    image: postgres:latest
    ports:
      - "5432"
    restart: always
    volumes:
      - db_volume:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: "latteoffice_user"
      POSTGRES_PASSWORD: "latteoffice_pass"
      POSTGRES_DB: "latteofficedb"
    networks:
      - latteofficeapi-dev
volumes:
  db_volume:

Dockerfile

##See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

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

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

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

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

Appsettings.json

{
  "ConnectionStrings": {
    "LatteOfficeDb": "Host=localhost;Port=5432;Username=latteoffice_user;Password=latteoffice_pass;Database=latteofficedb"
  },
}

Launchsettings.json

{
  "profiles": {
    "http": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5139"
    },
    "https": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7186;http://localhost:5139"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "latteofficeapi": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Container (Dockerfile)": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "environmentVariables": {
        "ASPNETCORE_HTTPS_PORTS": "5001",
        "ASPNETCORE_HTTP_PORTS": "5000"
      },
      "publishAllPorts": true,
      "useSSL": true
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56428",
      "sslPort": 44318
    }
  }
}
排查步骤
  • 统一端口配置:日志显示应用监听8080端口,但docker-compose映射的是5000:5000,Dockerfile暴露的也是5000,二者不匹配。要么在latteofficeapi服务的environment里添加ASPNETCORE_HTTP_PORTS=5000强制应用监听5000端口;要么把docker-compose的ports改成"5000:8080",访问地址改为localhost:5000/swagger。
  • 启用开发环境加载Swagger:默认.NET WebApi的Swagger仅在Development环境下启用,当前docker-compose未设置ASPNETCORE_ENVIRONMENT=Development,导致Swagger无法访问。在latteofficeapi服务的environment中添加该变量。
  • 验证容器内部访问:执行docker exec -it <容器ID> curl http://localhost:8080/swagger(替换为实际监听端口),检查容器内部能否访问Swagger,排除端口映射问题。
  • 检查数据库连接日志:虽然日志显示监听端口,但可能数据库连接失败导致应用未完全启动。执行docker logs <容器ID>查看完整日志,确认是否有数据库连接错误。
  • 验证Docker网络连通性:执行docker network inspect latteofficeapi-dev,确认两个容器都在同一网络内;再进入latteofficeapi容器执行ping postgres_image,验证服务间通信是否正常。
  • 忽略Launchsettings.json:容器部署时该文件不生效,应用端口由环境变量或代码配置决定,无需依赖该文件的配置。

内容的提问来源于stack exchange,提问作者halilenver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 21:57:07