/errors/curl/failed-to-connect-connection-refused
curl: (7) Failed to connect to localhost port 8080: Connection refused
- 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:
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:
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/
waitOutput
ok
Checklist
| Check | Command |
|---|---|
| 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 localhost | curl -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 log | curl -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.