Search code examples
windowsdocker.net-coredocker-compose

Can't access .net 8.0 API from docker compose


I'm playing around with Docker and trying to learn it. My goal is to create a micro-service setup with multiple .net core API's and an Angular frontend.

I've started with 1 API. I made a dockerfile and a docker-compose file.

my 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 8080
EXPOSE 8081

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

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

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

My docker-compose file:

version: '3.4'

services:
  todo.api:
    image: todoapi
    container_name: todo.api
    build:
      context: ./TestProject.Service.Todo
      dockerfile: Api/Dockerfile
    ports:
      - "5000:5000"
      - "5001:5001"

Whenever I start the docker-compose it runs and creates a container without issue but when I try to access the "WeatherForecast" api method that is added by default in the API project I can't access it.

When I run Visual Studio with the Docker option it also creates the container without issue and I can access the "WeatherForecast" api method without issues.

I currently can't see what is wrong with my current basic setup, could someone help me out with spotting the issue?

I googled the issue but didn't seem to find a working solution to my issue.


Solution

  • There are a couple of things that might cause this

    One issue that you definitely have is that dockerized .NET 8 apps listen on port 8080 for HTTP traffic. By default, HTTPS is not enabled. So you need to map port 8080 instead of 5000 and 5001 as you've done.

    Another issue is that Swagger is only available in the Development environment by default (the code that sets it up like that is in your Program.cs file). Docker is not considered Development by default, so Swagger is normally not available.

    To enable Swagger, you can either change Program.cs or you can set the ASPNETCORE_ENVIRONMENT environment variable to Development.

    If you change your compose file to

    version: '3.4'
    
    services:
      todo.api:
        image: todoapi
        container_name: todo.api
        build:
          context: ./TestProject.Service.Todo
          dockerfile: Api/Dockerfile
        ports:
          - "8080:8080"
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
    

    You should then be able to access Swagger on http://localhost:8080/swagger and the API on http://localhost:8080/weatherforecast.