Skip to content

Troubleshooting guide

Docker "port is already allocated": how to fix it

Docker cannot publish a host port that something else holds. The holder is another container, a host process, or a stale leftover.

3 min read11 sectionsUpdated

On this page

Key points

  • Look at both containers and host processes.
  • Stopped Compose projects can keep names but not ports; running ones keep ports.
  • Only the host side of a mapping must be unique.
  • Restarting the Docker daemon clears stale proxy bindings.

What it means

Mapping -p 8080:80 asks Docker to bind host port 8080. Only one listener may hold an address and port pair, so this fails if another process has it. Inside containers, ports are separate: ten containers can all use port 80 internally.

Common causes

  • Another container already publishes the port.
  • A second Compose project in another directory uses the same mapping.
  • A local service, such as Postgres on 5432 or Nginx on 80.
  • A stale docker-proxy process left after a crash.
  • On macOS, AirPlay Receiver listens on ports 5000 and 7000.

Diagnose it

Ask Docker first, then the operating system.

bash
# Which container publishes it?
docker ps --filter publish=8080 --format '{{.Names}}  {{.Ports}}'

# Which host process holds it?
sudo lsof -nP -iTCP:8080 -sTCP:LISTEN
sudo ss -ltnp | grep ':8080'

# All Compose projects on this machine
docker compose ls

Fixes

Stop whichever container or service holds the port, or map another: -p 8081:80. In Compose, make the port configurable: "${APP_PORT:-8080}:80", so two checkouts can run side by side. Use docker compose down rather than stop when you are finished. If nothing holds the port and the error persists, restart the Docker daemon to clear stale state. For a service that does not need outside access, do not publish a port at all, and let containers talk over a Docker network.

Why this happens on servers

On production hosts the conflict is usually an orchestration issue. A blue-green script that starts a new container before stopping the old one cannot share a host port, so publish different host ports and switch a reverse proxy between them. On a developer machine, the conflict tends to come from a database installed natively and another copy started in Compose. Remove one, or change the host side of the mapping, and remember to update the application's connection string to the new port.

Catch it automatically

In production this usually shows up as a deployment where the new container cannot start while the old one still runs. Use a health-gated rollout and an uptime check after each deploy. Monitor the restart count with container monitoring.

Frequently asked questions

Can two containers use the same internal port?

Yes. Only host port mappings must be unique.

Why does it say allocated when nothing is running?

A stale docker-proxy or a process outside Docker may hold it. Check with lsof or ss, or restart Docker.

How do I see all published ports?

Run docker ps and look at the Ports column.

Check it now, free

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.