C
C#7mo ago
Patricius

Dockerized .NET8 API not accessible.

When I try curl I get "(56) Recv Failure. Connection refused by peer." Docker compose
version: '2.27'
services:
api:
build:
context: ./server
dockerfile: Dockerfile
container_name: "dotnet_api"
restart: always
labels:
- "author=${AUTHOR}"
- "project=${SLUG}"
ports:
- "8080:8080"
environment:
ConnectionStrings__DefaultConnection: "server=db;userid=${MYSQL_USER};password=${MYSQL_PASSWORD};database=${MYSQL_DATABASE};"
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_URLS: "http://+:8080"
version: '2.27'
services:
api:
build:
context: ./server
dockerfile: Dockerfile
container_name: "dotnet_api"
restart: always
labels:
- "author=${AUTHOR}"
- "project=${SLUG}"
ports:
- "8080:8080"
environment:
ConnectionStrings__DefaultConnection: "server=db;userid=${MYSQL_USER};password=${MYSQL_PASSWORD};database=${MYSQL_DATABASE};"
ASPNETCORE_ENVIRONMENT: "Development"
ASPNETCORE_URLS: "http://+:8080"
Dockerfile
ARG DOTNET_IMAGE=mcr.microsoft.com/dotnet/sdk:8.0
FROM $DOTNET_IMAGE AS base
WORKDIR /usr/src/app

FROM base AS dependencies
COPY *.csproj ./
RUN dotnet restore

FROM base AS development
COPY --from=dependencies /usr/src/app/*.csproj ./
COPY . .
RUN dotnet build -c Debug -o out
EXPOSE 8080

ENTRYPOINT ["dotnet", "watch", "run", "--project", "server.csproj"]
ARG DOTNET_IMAGE=mcr.microsoft.com/dotnet/sdk:8.0
FROM $DOTNET_IMAGE AS base
WORKDIR /usr/src/app

FROM base AS dependencies
COPY *.csproj ./
RUN dotnet restore

FROM base AS development
COPY --from=dependencies /usr/src/app/*.csproj ./
COPY . .
RUN dotnet build -c Debug -o out
EXPOSE 8080

ENTRYPOINT ["dotnet", "watch", "run", "--project", "server.csproj"]
What I already tried: - Running it outside Docker, it works. - Running curl commands inside Docker, they work. - Made sure that my app listens to port 8080. - Changing port from 5282 to 8080. Port 5282 now gets failure 7, 8080 now gets error 56 similarly to what 5282 was getting. "Recv failure. Connection refused by peer." - Researched online, from what I understand this issue is .NET8 specific and has to do with "Kestrel" not allowing traffic from outside of local device. I am just using the weatherforecast template, so I have not written any code yet. I am on MacOS Sonoma 14.5 if that helps.
11 Replies
tera
tera7mo ago
why is entrypoint dotnet watch run? it should be dotnet Name.dll probably out/server.dll and build should probably be Release not Debug and you should dotnet publish after build so typically dotnet restore ... dotnet build ... dotnet publish ... dotnet app.dll i dont know if watch run could be the cause somehow i doubt Check logs and what does it say what urls app is listening on when it starts? just to be sure that its properly set as expected and i would check if something is using the port.. in case dont know for mac.. ss or netstat? @Patricius for visibility
Patricius
PatriciusOP7mo ago
Hey, thank you for answering, I was in a meeting. - Why is entrypoint dotnet watch run? This is supposed to be a development environment Dockerfile, dotnet watch is just setting up hot reload. I have another Dockerfile for prod but that one is facing the same issues. - Why Dockerfile for dev env? Because I have other pieces of Software and with Docker launching the project takes 1 command. - This is also why I dont do publish and dont use server.dll. I can assure you that outside of Docker these commands and how I run it work fine. Even inside of Docker when I run curl commands everything is fine. - What do logs say? The logs just show the typical dotnet startup screen:
dotnet_api | Determining projects to restore...
dotnet_api | All projects are up-to-date for restore.
dotnet_api | server -> /usr/src/app/bin/Debug/net8.0/server.dll
dotnet_api | dotnet watch 🚀 Started
dotnet_api | warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
dotnet_api | Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'http://localhost:8080'.
dotnet_api | info: Microsoft.Hosting.Lifetime[14]
dotnet_api | Now listening on: http://localhost:8080
dotnet_api | dotnet watch 🌐 Unable to launch the browser. Navigate to http://localhost:8080
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Application started. Press Ctrl+C to shut down.
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Hosting environment: Development
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Content root path: /usr/src/app
dotnet_api | Determining projects to restore...
dotnet_api | All projects are up-to-date for restore.
dotnet_api | server -> /usr/src/app/bin/Debug/net8.0/server.dll
dotnet_api | dotnet watch 🚀 Started
dotnet_api | warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
dotnet_api | Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'http://localhost:8080'.
dotnet_api | info: Microsoft.Hosting.Lifetime[14]
dotnet_api | Now listening on: http://localhost:8080
dotnet_api | dotnet watch 🌐 Unable to launch the browser. Navigate to http://localhost:8080
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Application started. Press Ctrl+C to shut down.
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Hosting environment: Development
dotnet_api | info: Microsoft.Hosting.Lifetime[0]
dotnet_api | Content root path: /usr/src/app
- Check if something is using the port? I have tried using 5282, 3000 (which I use for other apps I develop) and 8080. I know for sure that nothing else is using 3000.
tera
tera7mo ago
try with ASPNETCORE_URLS http://0.0.0.0:8080 instead
Patricius
PatriciusOP7mo ago
Same result:
curl http://localhost:8080/weatherforecast
curl: (56) Recv failure: Connection reset by peer
curl http://localhost:8080/weatherforecast
curl: (56) Recv failure: Connection reset by peer
As far as I know the error 56 shows that connection was established but dropped right away. This error also only occurs on the ports the app listens to, other ports throw error 7.
tera
tera7mo ago
and in logs? Now listening on... line
Patricius
PatriciusOP7mo ago
Logs look fine, listening on localhost:8080 as per usual.
tera
tera7mo ago
its not fine. it should say 0.0.0.0 not localhost
Patricius
PatriciusOP7mo ago
let me change the host within my app and see oh wow it works I hate dotnet but more myself for not thinking of this But also localhost and 0.0.0.0 was kind of in my head as the same thing
tera
tera7mo ago
localhost mean loop interface 0.0.0.0 mean all interfaces
Patricius
PatriciusOP7mo ago
Okay, thank you very much for resolving this with me. God knows how long it would take me to see this. Also, amazing cat u have
tera
tera7mo ago
np!
Want results from more Discord servers?
Add your server