Dockerized .NET API 未响应本地主机上的 GET 请求

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

我有一个在容器中运行的 Dockerized .NET API。但是,当我向 API 端点(例如,http://localhost:5000/api/trips)发出 GET 请求时,它不会返回任何响应。

相关代码片段: docker-compose.yml

services:
   mydb:
     container_name: my-db
     image: mcr.microsoft.com/mssql/server:2022-latest
     environment:
       - ACCEPT_EULA=Y
       - SA_PASSWORD=Password12345!
     networks: 
       - backend
     ports:
       - "1433:1433"
     volumes:
       - sql_data:/var/opt/mssql

   rest_api:
     container_name: my-api
     build:
       context: .
       dockerfile: rest_api/Dockerfile
     ports:
       - "5000:80"
       - "8081:443"
     environment:
       - ASPNETCORE_ENVIRONMENT=Development
     networks: 
       - backend
     depends_on:
       - mydb

    volumes:
      sql_data:
        driver: local

    networks:
      backend:
        driver: bridge

docker文件:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

WORKDIR /app

COPY rest_api/*.csproj ./rest_api/

RUN dotnet restore ./rest_api/rest_api.csproj

COPY . ./

RUN dotnet build ./rest_api/rest_api.csproj -c Release -o /app/build

FROM build AS publish
RUN dotnet publish ./rest_api/rest_api.csproj -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "rest_api.dll"]

控制器

[Route("api/trips")]
[ApiController]
public class TripsController : ControllerBase
{
    private readonly IServiceManager _service;

    public TripsController(IServiceManager service) => _service = service;

    [HttpGet]
    public async Task<IActionResult> GetTrips()
    {
        var trips = await _service.TripService.GetAllTripsAsync(trackChanges:        false);
        return Ok(trips);
    }
}

我正在使用.Net 8

这是我到目前为止所做的:

检查Docker Compose日志:我检查了rest_api容器的日志,但没有发现任何明显的错误消息。 已验证的 API 端点:我已经仔细检查了控制器中 api/trips 端点的实现,它似乎已使用 [HttpGet] 属性和适当的操作方法正确配置。 检查 Docker Compose 配置:我已验证 docker-compose.yml 中的端口映射是否正确(例如 5000:80)。 使用curl进行测试:我尝试使用curl直接发出请求,但它也没有返回响应。 重新启动容器:我已经重新启动了rest_api容器,但问题仍然存在。

当我运行时:docker log my-api 我得到:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
  Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed. For more information go to https://aka.ms/aspnet/dataprotectionwarning
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
  No XML encryptor configured. Key {518f52c6-dd78-4287-ae48-641ec1f8ccfa} may be persisted to storage in unencrypted form.
info: Microsoft.Hosting.Lifetime[14]
  Now listening on: http://[::]:8080
info: Microsoft.Hosting.Lifetime[0]
  Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
  Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
  Content root path: /app

任何有关如何解决此问题的见解或建议将不胜感激!

sql-server docker docker-compose dockerfile .net-8.0
1个回答
0
投票

正如您在日志消息

Now listening on: http://[::]:8080
中看到的,您的应用程序正在端口 8080 上侦听 HTTP 流量。这是 dockerized .NET 8+ 应用程序的默认行为。

要使其正常工作,您可以更改 docker compose 端口映射

 ports:
   - "5000:80"
   - "8081:443"

 ports:
   - "5000:8080"

然后您就可以通过

http://localhost:5000/api/trips
访问您的 API。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.