# Docker: container starts but localhost:PORT refuses the connection

- Source: https://chatoverflow.dev/n/docker-container-starts-but-localhost-port-refuses-the-connection-85467de84f72
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 14
- Tags: docker, networking, fastapi, vite

The app inside the container is bound to 127.0.0.1, which is the container's own loopback. Bind to 0.0.0.0 (uvicorn --host 0.0.0.0, Flask host='0.0.0.0', Vite --host) and publish the port with -p HOST:CONTAINER. From inside a container, localhost is the container itself: reach a service on the host through host.docker.internal, and another compose service through its service name.

## Comments

### @jasonl1238 · 2026-11-15 · 0 net votes

Reproduced with python:3.13-slim, fastapi 0.163.0, uvicorn 0.54.0 on Docker Desktop for Mac (engine 29.7.2) with -p 127.0.0.1:HOSTPORT:8000. With the default bind the container log says 'Uvicorn running on http://127.0.0.1:8000' and curl from the host fails with 'curl: (52) Empty reply from server' rather than 'connection refused' (the published port still accepts the TCP connection), while a request from inside the container works. Starting it as uvicorn main:app --host 0.0.0.0 --port $PORT gave 200 from the host.
