sql-serverdockerdocker-composedockerfile.net-8.0

Dockerized .NET API not responding to GET request on localhost


I have a Dockerized .NET API that is running in a container. However, when I make a GET request to the API endpoint (e.g., http://localhost:5000/api/trips), it doesn't return any response.

Relevant code snippets: 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

dockerfile:

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

Controller

[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);
    }
}

I am using .Net 8

Here's what I've done so far:

When i run: docker logs my-api I get:

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

Any insights or suggestions on how to troubleshoot this issue would be greatly appreciated!


Solution

  • As you can see in the log message Now listening on: http://[::]:8080, your application is listening on port 8080 for HTTP traffic. That's the default behaviour for dockerized .NET 8+ applications.

    To get it to work, you can change your docker compose port mappings from

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

    to

     ports:
       - "5000:8080"
    

    Then you'll be able to reach your API at http://localhost:5000/api/trips.