Skip to main content
⚡ OpenDevDocs
Docker Errors
devopsdockerTroubleshootingIntermediate

Docker: Cannot connect to database from container (ECONNREFUSED / localhost)

Fix database connection errors from Docker containers caused by using localhost instead of bridge network service names or host.docker.internal.

Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#networking#database#postgresql#errors

Error Message

connect ECONNREFUSED 127.0.0.1:5432

or

psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused

Symptoms

  • An application container crashes on startup when attempting to connect to PostgreSQL, MySQL, Redis, or MongoDB.
  • You can connect to the database from your host machine via DBeaver / psql, but the app inside Docker fails to connect.

Why It Happens

Inside a Docker container, localhost (127.0.0.1) points to the container's own isolated network namespace, NOT the host machine and NOT other containers!

┌───────────────────────────┐      ┌───────────────────────────┐
│ Container A (API Server)  │      │ Container B (PostgreSQL)  │
│ localhost:5432 (Nothing!) │ ──X─►│ localhost:5432 (Listening)│
└───────────────────────────┘      └───────────────────────────┘

Solutions

Use the service name as the hostname. Docker Compose automatically creates a shared DNS bridge:

services:
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: secret

  api:
    build: .
    environment:
      # Use "db" hostname instead of "localhost"
      - DATABASE_URL=postgresql://postgres:secret@db:5432/postgres
    depends_on:
      - db

Solution 2: If the Database is Running Natively on Your Host OS

On macOS & Windows (Docker Desktop):

Use the special DNS name host.docker.internal:

DATABASE_URL=postgresql://postgres:secret@host.docker.internal:5432/postgres

On Linux:

Pass --add-host=host.docker.internal:host-gateway to docker run:

docker run -d --add-host=host.docker.internal:host-gateway \
  -e DATABASE_URL="postgresql://postgres:secret@host.docker.internal:5432/postgres" \
  my-app

On this page