Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

ASP.NET Core container does not answer on the published port

Errorcurl: (56) Recv failure: Connection reset by peer

Short answer

Since .NET 8 the ASP.NET Core container images listen on port 8080, not 80. A mapping such as -p 8000:80 now points at nothing. Map to 8080, or set ASPNETCORE_HTTP_PORTS to the port you want. Also make sure the app binds to all interfaces, not only localhost.

  • Applies to .NET 8, .NET 9, Docker Engine 28, Docker Compose v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Show which container port is mapped, then look at the app's own startup log. The port the app listens on must equal the right-hand side of the mapping.

    docker port my-app
    docker logs my-app
  2. 2Match what you see, then jump to its fix

    1. 1The mapping ends in :80 and the image is based on an 8.0 or newer ASP.NET imageThe mapping still targets port 80Fix
    2. 2ASPNETCORE_HTTP_PORTS is set but the app still does not answer; startup code calls WebHost.CreateDefaultBuilderThe app uses the older WebHost builder and ignores the new variableFix
    3. 3The log or configuration shows a URL like http://localhost:5000, and the connection from the host is reset or refusedThe app is bound to localhost inside the containerFix

What does the error look like?

  • curl: (56) Recv failure: Connection reset by peer
  • The container is running and logs no error, but the mapped host port never answers
  • Worked on .NET 7, stopped after changing the base image to 8.0

Cause 1 of 3

The mapping still targets port 80

1Check

The images moved the default listening port to 8080 so the app can run as a non-root user. Port 80 is a privileged port. A mapping to container port 80 reaches no listener.

docker inspect --format '{{.Config.Env}}' my-app

2Fix

Map to 8080. If other tools expect port 80, set ASPNETCORE_HTTP_PORTS to 80 instead. Change the same number in Compose or Kubernetes manifests.

docker run --rm -d -p 8000:8080 my-app
# or keep 80 inside the container
docker run --rm -d -p 8000:80 -e ASPNETCORE_HTTP_PORTS=80 my-app

# compose.yaml
services:
  web:
    image: my-app
    ports:
      - "8000:8080"

3Verify

The published port answers from the host.

curl -I http://localhost:8000

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The app uses the older WebHost builder and ignores the new variable

1Check

Apps built with the older WebHost.CreateDefaultBuilder API do not read ASPNETCORE_HTTP_PORTS. Because ASPNETCORE_URLS is no longer set by the image, they fall back to http://localhost:5000.

grep -rn "CreateDefaultBuilder" --include=*.cs .

2Fix

Set ASPNETCORE_URLS, not ASPNETCORE_HTTP_PORTS, for these apps.

docker run --rm -d -p 8000:80 -e ASPNETCORE_URLS=http://*:80 my-app

3Verify

Request the mapped port again.

curl -I http://localhost:8000

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The app is bound to localhost inside the container

1Check

A listener on localhost accepts connections only from the same network namespace. Docker forwards the published port to the container's network address, so a localhost-only app is never reached. Look for urls in appsettings.json, ASPNETCORE_URLS and launchSettings-derived values.

docker exec my-app printenv | grep -i aspnetcore
grep -rn "localhost" appsettings*.json

2Fix

Bind to every interface: a wildcard host in the URL, or just the port variable.

ENV ASPNETCORE_HTTP_PORTS=8080
# or
ENV ASPNETCORE_URLS=http://*:8080

3Verify

Rebuild, rerun and request the published port.

docker build -t my-app .
docker run --rm -d -p 8000:8080 my-app
curl -I http://localhost:8000

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Write the container port once (ASPNETCORE_HTTP_PORTS in the Dockerfile) and reuse that number in run commands, Compose and Kubernetes.
  • After moving a base image to a new major version, read its breaking-changes page before the first deploy.
  • On Azure container hosts, set the target port the same way (for App Service, WEBSITES_PORT).

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.