使用 docker-compose 构建后无法访问我的 webapi

问题描述 投票:0回答:1

我有一个带有 postgres 数据库的 .net 8 WebApi。 我想在 docker 上运行它,但我的 docker-compose 配置不起作用。 当我调用“docker-compose up”API 时,postgres 在 docker 上运行。 在 webapi 的 docker 日志中,它显示“现在正在监听:http://[::]:8080” 但我无法从 localhost:8080/swagger 或 localhost:5000/swagger 访问 webapi 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:

Docker 文件

##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"]

应用程序设置.json

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

启动设置.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
    }
  }
}
postgresql docker docker-compose webapi .net-8.0
1个回答
0
投票

正如您在日志中看到的,该应用程序正在侦听端口 8080。但是您将端口 5000 映射到主机端口 5000,因此这并不一致。

如果将端口映射更改为

ports:
  - "8080:8080"     

您应该能够在

http://localhost:8080/
上访问该应用程序。

请注意,Swagger 默认情况下不可用。 Swagger 仅在开发模式下启用,当您在容器中运行应用程序时,这不被视为开发。您可以通过将环境变量 ASPNETCORE_ENVIRONMENT 设置为“Development”来更改它。然后 Swagger 就可用了。

您还应该知道 launchSettings.json 是一个 Visual Studio 文件,仅在您从那里启动项目时使用。在那里,您说您希望应用程序侦听端口 5000,但正如您所发现的,在使用 compose 运行项目时会忽略这一点。

© www.soinside.com 2019 - 2024. All rights reserved.