🎉 DevOps Interview Prep Bundle is live — 1000+ Q&A across 20 topicsGet it →
All Fixes
Today I Fixed

docker compose up failing with 'Bind for 0.0.0.0:5432 failed: port is already allocated'

dockerJul 2, 20265 minutes to fixdockertroubleshooting

Ran docker compose up and got: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 0.0.0.0:0: listen tcp 0.0.0.0:5432: bind: address already in use.

Root cause: A previous docker compose up run wasn't cleanly stopped — the container was still running in the background with the port bound. Alternatively, a local PostgreSQL service installed on the host was already listening on 5432.

Fix:

First, bring down any existing Compose stack cleanly:

bash
docker compose down

If that doesn't free the port, find what process is holding it:

bash
# Linux / macOS
lsof -i :5432
# or
ss -tlnp | grep 5432
 
# Windows
netstat -ano | findstr :5432

Kill the process or stop the conflicting service:

bash
# If it's a local postgres service
sudo systemctl stop postgresql
 
# If it's a stale Docker container
docker ps -a | grep 5432
docker rm -f <container_id>

If you want to keep both running, map to a different host port in compose.yaml:

yaml
services:
  db:
    image: postgres:16
    ports:
      - "5433:5432"   # host:container — changed host port to 5433

Lesson: Always run docker compose down before docker compose up if you're unsure of the current state — orphaned containers from previous sessions are the most common cause of port conflicts.