Skip to content

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

Key points

  • Refused is a fast, definite answer: the host is reachable, the port is closed.
  • In Docker, localhost is the container itself, not your laptop or another container.
  • Check the address and port in the error first.
  • Retry with backoff at startup, since dependencies may start later.

Causes, most likely first

CauseCheck
The service is not runningsystemctl status, docker ps, kubectl get pods
Wrong portss -ltnp | grep <port> on the target
Wrong host: localhost inside a containerUse the service name (db, redis) or host.docker.internal
Service listens only on 127.0.0.1Bind to 0.0.0.0 (with a firewall) if others must connect
IPv6 vs IPv4 mismatchlocalhost resolving to ::1 while the service listens on 127.0.0.1 only
Dependency still startingStartup order; add retries or health checks

The Docker case

compose.yamlyaml
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:7

The Node.js 17+ localhost change

Node 17 and later stopped reordering DNS results to prefer IPv4, so localhost may resolve to ::1. If the service listens only on 127.0.0.1, you get ECONNREFUSED ::1. Use 127.0.0.1 explicitly or make the service listen on both.

Testing the connection

bash
nc -vz db 5432          # from inside the calling container
curl -v http://api:8080/health
ss -ltnp                # on the target: what is listening, and on which address

Catching 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.

The most common ECONNREFUSED situations

SituationWhy it happensFix
Connecting to localhost inside a containerlocalhost is the container itself, not the host or another containerUse the service name (Docker Compose) or host.docker.internal
Service listens on 127.0.0.1 onlyIt refuses connections from other machinesBind to 0.0.0.0 or the right interface
App starts before its databaseThe dependency is not listening yetRetry with backoff at startup, or wait for a health check
Wrong port in configurationNothing listens thereCheck the environment variable the app actually reads
Node 17+ resolving localhost to ::1The server listens on IPv4 onlyUse 127.0.0.1 explicitly, or listen on both
bash
# is anything listening on the port, and on which address?
ss -ltnp | grep 5432
# can this machine reach it?
nc -vz db.internal 5432

Prevent 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.

Frequently asked questions

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

  • 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.