Error explained
Error: connect ECONNREFUSED
ECONNREFUSED means the target machine answered "nothing is listening on that port". The service is down, on another port, or you are connecting to the wrong host.
4 min read13 sectionsUpdated
On this page
| Cause | Check |
|---|---|
| The service is not running | systemctl status, docker ps, kubectl get pods |
| Wrong port | ss -ltnp | grep <port> on the target |
| Wrong host: localhost inside a container | Use the service name (db, redis) or host.docker.internal |
| Service listens only on 127.0.0.1 | Bind to 0.0.0.0 (with a firewall) if others must connect |
| IPv6 vs IPv4 mismatch | localhost resolving to ::1 while the service listens on 127.0.0.1 only |
| Dependency still starting | Startup order; add retries or health checks |
services:
api:
build: .
environment:
DATABASE_URL: postgres://app:secret@db:5432/app # "db", not "localhost"
REDIS_URL: redis://redis:6379
depends_on:
db: { condition: service_healthy }
db:
image: postgres:16
healthcheck: { test: ["CMD", "pg_isready", "-U", "app"], interval: 5s, retries: 10 }
redis:
image: redis:7Catching it in production
A refused connection to a dependency usually shows as a spike of errors on every endpoint that uses it. Traces show the failing client span with the target address; a TCP or Postgres/Redis check from outside the app confirms whether the dependency itself is down.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
In Kubernetes
Connect to a Service by its DNS name (postgres.db.svc.cluster.local or postgres within the same namespace). A refused connection there usually means no ready pod behind the Service, or the Service's targetPort does not match the container port. kubectl get endpoints <service> shows whether any pod addresses are behind it.
| Situation | Why it happens | Fix |
|---|---|---|
| Connecting to localhost inside a container | localhost is the container itself, not the host or another container | Use the service name (Docker Compose) or host.docker.internal |
| Service listens on 127.0.0.1 only | It refuses connections from other machines | Bind to 0.0.0.0 or the right interface |
| App starts before its database | The dependency is not listening yet | Retry with backoff at startup, or wait for a health check |
| Wrong port in configuration | Nothing listens there | Check the environment variable the app actually reads |
| Node 17+ resolving localhost to ::1 | The server listens on IPv4 only | Use 127.0.0.1 explicitly, or listen on both |
# is anything listening on the port, and on which address?
ss -ltnp | grep 5432
# can this machine reach it?
nc -vz db.internal 5432Prevent it in production
Use health checks and readiness probes so traffic only reaches instances whose dependencies are reachable, and alert on connection errors per dependency. A burst of ECONNREFUSED to the database right after a deploy usually means a configuration change, not an outage. See Docker container monitoring for container setups.
What is the difference between ECONNREFUSED and ETIMEDOUT?
Refused means the host answered that the port is closed. Timed out means no answer came at all, usually a firewall or an unreachable host.
Why does it work locally but not in Docker?
Inside a container, localhost refers to that container. Use the other container’s service name on the shared network.
Check it now, free
- Open port checker: Is a TCP port open, closed or filtered from the internet?
Related reading
- Redis connection refused: how to fix it: Fix Redis connection refused on port 6379: server stopped, bind address, protected mode and Docker networking, with redis-cli and ss commands for each cause.
- Sentry vs Datadog: Sentry vs Datadog compared: developer-focused error tracking against a full observability platform, covering errors, tracing, replay, logs and cost.
- Cloudflare error 523: origin is unreachable: Fix Cloudflare error 523 Origin is Unreachable: wrong DNS IPs, routing problems at the host and firewalls, with tests that confirm where traffic stops.
- Cloudflare error 1016 "Origin DNS error": how to fix it: Fix Cloudflare error 1016 Origin DNS error: CNAME records pointing to names that do not resolve, deleted origins and load balancer pools, with checks.
- Error: socket hang up: Why Node.js reports "socket hang up", from servers closing connections and timeouts to proxies and TLS problems, with steps to find the cause.
- React performance monitoring: Monitor React apps in production: Core Web Vitals from real users, slow renders and INP, bundle size, errors with source maps.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.