Search code examples
.netwindowsdockerwebapi.net-8.0

dotnet 8 web api not working after deploying to docker


I have a .NET 8 project. Its a simple web API with the default WeatherForecastController.

Here is the project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <InvariantGlobalization>true</InvariantGlobalization>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
  </ItemGroup>

</Project>

I have added docker support through the add container orchestration support option. So I got the docker-compose.yml file and a DockerFile.

enter image description here

Here is my launchsettings.json file.

{
  "profiles": {
    "http": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "http://localhost:5071"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
      "environmentVariables": {
        "ASPNETCORE_HTTP_PORTS": "8081"
      },
      "publishAllPorts": true
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:22910",
      "sslPort": 0
    }
  }
}

This is the 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 8081

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

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

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

This is the docker-compose.yml file

version: '3.4'

services:
  dockertest2:
    image: ${DOCKER_REGISTRY-}dockertest2
    build:
      context: .
      dockerfile: DockerTest2/Dockerfile

This is the docker-compose.override.yml

 version: '3.4'

services:
  dockertest2:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_HTTP_PORTS=8080
    ports:
      - "8081:80"

Here the problem is after I deploy, I am not able to browse the api swagger pages. Even the api direct call also not working.

enter image description here

What went wrong here. How can I troubleshoot? Logs are all fine.


Solution

  • You need to use matching ports. For example if you start the service with compose - ASPNETCORE_HTTP_PORTS=8080 will result in app listening on 8080 port, but you are mapping the 80 port of the container, fix that:

    services:
      dockertest2:
        # ...
        ports:
          - "8081:8080"
    

    See also: