I have the image built like this using executing
docker build --tag=api --file=Dockerfile_01 --no-cache --progress=plain .
based on the Dockerfile_01 below.
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /bin
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish --configuration Release --output /release
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /bin
COPY --from=build /release .
EXPOSE 8080 8081
CMD ["dotnet", "Api.dll"]
I start my container executing
docker run --detach --publish 5001:8080 --publish 7001:8081 --name WebApi api
and it seems to work, the log tells me that the program listens to port 8080 etc. However, when I navigate to http://localhost:5001 I get 404. When I fire directly from VS using the corresponding profile, I get to see Swagger, as expected.
How can I troubleshoot it?
The only unexpected thing I see is in the log, telling my that the redirection to secure protocol failed due to some confusion on ports. That's a tomorrow's problem, though, and I try to go for for 5001 which is HTTP, for now.
2025-02-14 17:56:41 info: Microsoft.Hosting.Lifetime[14]
2025-02-14 17:56:41 Now listening on: http://[::]:8080
2025-02-14 17:56:41 info: Microsoft.Hosting.Lifetime[0]
2025-02-14 17:56:41 Application started. Press Ctrl+C to shut down.
2025-02-14 17:56:41 info: Microsoft.Hosting.Lifetime[0]
2025-02-14 17:56:41 Hosting environment: Production
2025-02-14 17:56:41 info: Microsoft.Hosting.Lifetime[0]
2025-02-14 17:56:41 Content root path: /usr/bin
2025-02-14 17:57:02 warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
2025-02-14 17:57:02 Failed to determine the https port for redirect.
I suspect that there's something fishy with how I configured the ports but I fail to see where. It follows the suggestion from this answer, as well. I tested a bunch of different combinations but didn't notice anything helpful.
Naturally, I tested /, /swagger and /swagger.html just to be sure, although I know it's not the issue.
If you look in your Program.cs file, you'll see that Swagger is only configured if the app is running in a development environment. The default for containerized applications is to be considered production code.
To get Swagger to be available, you can set ASPNETCORE_ENVIRONMENT to 'Development' on your run command like this
docker run -e ASPNETCORE_ENVIRONMENT=Development --detach --publish 5001:8080 --publish 7001:8081 --name WebApi api
When you run your container from VS, VS sets the environment to development, so that's why it works from there.