← Back to all fixes

Container cannot reach host or other containers

The Issue

App inside container tries to connect to `localhost:5432` (postgres) but gets connection refused.

Root Cause

Inside a container, `localhost` refers to the container itself, not the host machine. Sibling containers are also not reachable via `localhost`.

How to Fix
  1. For host services on Linux: use `172.17.0.1` (default docker bridge gateway) or `--network host`
  2. For sibling containers: use Docker Compose and reference by service name, e.g. `postgres:5432`
  3. Check which network the container is on: `docker inspect <container> | grep NetworkMode`
  4. Use `docker network ls` and `docker network inspect bridge` to see connected containers and their IPs