postgresqldockerdocker-composewebapi.net-8.0

Cannot access to my webapi after building it with Docker Compose


I have a .net 8 WebApi with Postgres database. I want to run it on docker but my docker-compose configuration does not work.

When I call "docker-compose up" API and Postgres runs on Docker. In webapi's Docker logs it says "Now listening on: http://[::]:8080" But I cannot access webapi swagger from localhost:8080/swagger or localhost:5000/swagger. I could not find the problem in my configs.

What can I try next?

Here is my config files:

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
    }
  }
}

Solution

  • As you see in your logs, the app is listening on port 8080. But you map port 5000 to host port 5000, so that doesn't align.

    If you change the port mapping to

    ports:
      - "8080:8080"     
    

    you should be able to access the app on http://localhost:8080/.

    Be aware that Swagger will not be available by default. Swagger is only enabled in development mode and when you run your app in a container, that's not considered development. You can change that by setting the environment variable ASPNETCORE_ENVIRONMENT to 'Development'. Then Swagger will be available.

    You should also be aware that launchSettings.json is a Visual Studio file and is only used when you launch your project from in there. In there you say you want the app to listen on port 5000, but as you've discovered, that's ignored when running the project using compose.