Skip to content

/errors/curl/failed-to-connect-connection-refused

curl: (7) Failed to connect to localhost port 8080: Connection refused

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · curl 8.21.0 (x86_64-w64-mingw32) libcurl/8.21.0 Schannel zlib/1.3.2updated
tool
curl
exit code
7
versions
curl 7.x, curl 8.x
os
linux, macos, windows

curl: (7) Failed to connect to HOST port N: Connection refused means curl resolved the host but the TCP connection was refused: no program accepted connections on that address and port. Exit code 7 is CURLE_COULDNT_CONNECT. Check that the server is running and listening on the port and address you are calling (ss -ltn on Linux), then retry.

Reproduce it

Nothing listens on port 4300:

bash
curl -sS http://127.0.0.1:4300/ 2>&1 | sed -E 's/after [0-9]+ ms/after N ms/'
echo "exit status: ${PIPESTATUS[0]}"

Output

curl: (7) Failed to connect to 127.0.0.1:4300 after N ms: Could not connect to server
exit status: 7

The wording changes between curl versions: older 7.x releases print Failed to connect to localhost port 8080: Connection refused, curl 8 adds the elapsed time (after T ms), and the reason text depends on the build and operating system. The curl 8.21 Windows build used here writes HOST:PORT and Could not connect to server. The exit code is 7 in every case.

Fix: connect to a listening port

Start a server, then call the port it listens on:

bash
node -e 'require("http").createServer((q, r) => r.end("ok\n")).listen(4301, "127.0.0.1"); setTimeout(() => process.exit(0), 3000)' &
sleep 1
curl -sS http://127.0.0.1:4301/
wait

Output

ok

Checklist

CheckCommand
Is anything listening? (Linux)ss -ltnp
Is anything listening? (macOS)lsof -nP -iTCP -sTCP:LISTEN
Is anything listening? (Windows)netstat -ano | findstr LISTENING
IPv4 vs IPv6 localhostcurl -4 http://localhost:8080/ and curl -6 http://localhost:8080/
Container port published?docker ps (look for 0.0.0.0:8080->8080/tcp)
Verbose connection logcurl -v http://host:port/

A server that prints listening on 127.0.0.1:8080 inside a Docker container accepts connections only from inside that container. Make it listen on 0.0.0.0 and publish the port.

Sources