Unable to properly launch Web API using Docker (Compose)

As the title suggests, I am having issues in running my Web API through Docker. When I launch the project and want to open it using localhost it is unreachable. My project structure: Solution/ - docker-compose (default) - docker-compose.override (default) - API/ - dockerfile The files: 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.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["test.API/test.API.csproj", "test.API/"]
RUN dotnet restore "./test.API/test.API.csproj"
COPY . .
WORKDIR "/src/test.API"
RUN dotnet build "./test.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./test.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "test.API.dll"]
# 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.

# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081


# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["test.API/test.API.csproj", "test.API/"]
RUN dotnet restore "./test.API/test.API.csproj"
COPY . .
WORKDIR "/src/test.API"
RUN dotnet build "./test.API.csproj" -c $BUILD_CONFIGURATION -o /app/build

# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./test.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "test.API.dll"]
Docker-compose:
services:
test.api:
image: ${DOCKER_REGISTRY-}testapi
build:
context: .
dockerfile: test.API/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8080:8080"
- "8081:8081"
services:
test.api:
image: ${DOCKER_REGISTRY-}testapi
build:
context: .
dockerfile: test.API/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "8080:8080"
- "8081:8081"
3 Replies
electronic heartbreak.
docker-compose.override
services:
test.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
ports:
- "8080"
- "8081"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
services:
test.api:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_HTTP_PORTS=8080
- ASPNETCORE_HTTPS_PORTS=8081
ports:
- "8080"
- "8081"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/home/app/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/home/app/.aspnet/https:ro
I am running the project in .NET 9. I even tried this project setup in a default weather forecast web api but still the same result. Before I pulled my hair out of frustration I wenr to the basics and tried a simple GET method: http://localhost:55720/weatherforecast this returned the "hello world" I gave it.
Raso
Raso2mo ago
Just to be sure, Are you trying to reach the http://localhost:8080 right? Without docker the port might be the 55720 but in your docker-compose you are telling that you are mapping for http 8080 your pc -> 8080 docker container, for https 8081 your pc -> 8081 docker container
electronic heartbreak.
Hey, I am sorry for my late reply. I have this discord muted and therefore did not managed to give you an answer. In my setup above; yes I tried reaching localhost on 8080. What I initially do in these kind of scenario's is to always map them equally. So it is always 8080:8080 & 8081:8081. Since this did not work, I removed the mappings and went back to the basics, displaying a "hello world" through a GET method.

Did you find this page helpful?