C
C#5mo ago
SOMUCHDOG

✅ .Net 8 WebApi not responding from Docker container

I am running a default .Net 8 webapi project that I created via the dotnet CLI
dotnet new webapi -n dotnetapi
dotnet new webapi -n dotnetapi
The project builds and runs locally, I am able to get a response. Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet build -c Release -o /app/build

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DotNetApi.dll"]
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore
RUN dotnet build -c Release -o /app/build

FROM build AS publish
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DotNetApi.dll"]
I build the image and run it
docker run -p 5154:80 dotnet-api
docker run -p 5154:80 dotnet-api
Cannot get a response from the api
curl http://localhost:5154/weatherforecast
curl http://localhost:5154/weatherforecast
Any help appreciated!
6 Replies
cap5lut
cap5lut5mo ago
what do the logs of the container say which port kestrel is listening on?
Pobiega
Pobiega5mo ago
Highly doubt kestrel is listening to 80 so yeah, check the container logs for the
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:xxxx
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:xxxx
message thats the port you need to expose and map up as your in-container port
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
SOMUCHDOG
SOMUCHDOGOP5mo ago
Thanks for the response guys.. I've been at work so I couldn't get answers out. Here's the logs. I'm just getting started with Docker, so I am trying to understand a bit here. I exposed 80, Kestrel is listening on 8080, so traffic is not getting to the container since it's listening on the wrong port?
No description
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut5mo ago
the -p 5154:80 part of the docker run command means that u tunnel/bind the host port 5154 to the container internal's port 80. Now listening on http://0.0.0.0:5154 means that its listening for any IP on port 5154, which is within the docker container. so basically, if u swap the ports to -p 80:5154, requests like http://localhost/weatherforecast would do the trick here, to make it work. and that weird port configuration, as TeBeCo already mentioned, should be investigated as well.
Want results from more Discord servers?
Add your server