curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server inside a Docker container means nothing listens on port 8080 in that container. Inside a container, localhost is the container itself, not your PC and not the container that runs the server.

If the server runs in another container on the same Docker network, call it by its name on that network: here http://web:8080/ instead of http://localhost:8080/.

The error

$ curl -sS http://localhost:8080/
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server
exit=7

Why it happens

The web server in this test ran in a container with the alias web; the curl above ran in a different container, the client. In the client, localhost pointed back at the client, where nothing listened on 8080 (a rerun with recreated containers shows the client's listening sockets), so curl could not connect (after 0 ms: Couldn't connect to server) and exited with code 7.

The server itself was fine. Run inside its own container, the same request on localhost answered 200:

$ curl -sS -o /dev/null -w '%{http_code}\n' http://localhost:8080/
200
exit=0

In the rerun, the client's own listening sockets held one line, on 127.0.0.11, and nothing on 8080, and the same error came back:

$ ss -ltn
State  Recv-Q Send-Q Local Address:Port  Peer Address:PortProcess
LISTEN 0      4096      127.0.0.11:42551      0.0.0.0:*
exit=0
$ curl -sS http://localhost:8080/
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server
exit=7

Code 7 is not a name problem: the name resolved and curl then could not connect. A name that does not resolve gives code 6 instead.

The fix

$ getent hosts web
172.20.0.4      web
exit=0
$ curl -sS -o /dev/null -w '%{http_code}\n' http://web:8080/
200
exit=0

From the client container, the server's network alias resolved to its address on the shared network and the request returned 200. The server was started as python3 -m http.server 8080, which listens on all of the container's interfaces; its log showed the request arriving from the client's address. A server that listens only on its own 127.0.0.1 would not be reachable this way; that case was not tested here.

How it was reproduced

Reproduced inside two ubuntu:24.04 containers on Docker engine 29.9.3, both attached to a user-defined network created with docker network create. The server container ran Python 3.12.3's http.server on port 8080 and had the network alias web; the client had curl 8.5.0 from apt-get install -y curl. The containers and the network were removed afterwards.

Frequently asked

Why does curl localhost fail inside a Docker container?
Inside a container, localhost is that container. In this test the server ran in another container, so curl found nothing listening on port 8080 and exited with code 7.
How do I curl another container in Docker?
Put both containers on the same user-defined network and use the server container's network alias in the URL, for example http://web:8080/. That returned 200 in this test.
What is the difference between curl error 6 and curl error 7?
Error 6 means the host name did not resolve to an address. Error 7 means the name resolved but the connection failed, here because nothing listened on the port.

If the name itself does not resolve, see curl: (6) Could not resolve host. The same localhost mistake with Ollama is covered in Ollama from a Docker container: connection refused on localhost. More decoded errors in the Fixes category.