ProjectStack
docker

connect: connection refused

A process inside your container tried to connect to a service or port that isn't listening. This commonly happens when a container tries to reach another service that hasn't started yet, is on the wrong network, or is listening on a different port.

Common causes

  • The target service (database, API, another container) isn't running or isn't ready yet
  • Connecting to localhost inside a container when the service is in another container — use the service name instead
  • The target container is on a different Docker network and can't be reached
  • The service is running but on a different port than what's configured in the app

How to fix it

  1. In Docker Compose, use the service name as the hostname: db instead of localhost for a database container
  2. Add a health check and depends_on to wait for the service to be ready before connecting
  3. Verify all containers are on the same network: docker network inspect NETWORK_NAME
  4. Check the target service is actually listening on the expected port: docker exec service-container ss -tlnp

Example

$ docker-compose up app_1 | Error: connect ECONNREFUSED 127.0.0.1:5432

App container tries to connect to localhost:5432, but the database is in a separate container — use the service name 'db' instead

Browse more errors

The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.