Skip to main content

Docker · 5 common causes

port is already allocated

Updated

Docker cannot publish the host port you asked for because something already holds it.

The exact message

$ docker compose up
[+] Running 1/1
 ! web Error
Error response from daemon: driver failed programming external connectivity on endpoint
web-1 (a3f1...): Bind for 0.0.0.0:8080 failed: port is already allocated

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is port is already allocated.

How to fix it

Cause 1 of 5

An earlier container still holds the mapping.

docker ps -a lists containers with their port mappings, including stopped ones that still reserve an endpoint. Stop and remove it — docker stop <name> then docker rm <name> — or bring the whole stack down cleanly with docker compose down.

Cause 2 of 5

A process outside Docker owns the port.

lsof -i :8080 on macOS or Linux, netstat -ano | findstr :8080 on Windows, names the process. Stop that service, or publish your container on a different host port instead of contesting it.

Cause 3 of 5

You can simply move the host side of the mapping.

-p 8081:8080 — the left number is the host and the right is the container. Nothing inside the image or the application configuration changes, which makes this the lowest-risk fix.

Cause 4 of 5

Two services in the same compose file publish the same host port.

Only one can win. Give each a distinct host port, or remove ports: from the service that only needs to be reachable from inside the stack — containers reach each other by service name on the compose network without publishing anything to the host.

Cause 5 of 5

The daemon holds a stale endpoint after a crash.

docker compose down followed by up again clears it in most cases. If the port is still claimed with no container to match, restarting the Docker daemon releases endpoints it wrongly believes are live.

Stopping it coming back

Give each project a distinct host port range in its compose file, and prefer the compose network over published ports for anything that does not need to be reachable from your machine.

port is already allocated — common questions

Which side of -p is the host?
The left. -p 8081:8080 publishes container port 8080 on host port 8081. Getting them the wrong way round produces a container that starts but is unreachable, which is a quieter failure than this one.
Why does the port stay allocated after the container stopped?
A stopped container keeps its network endpoint until it is removed. docker rm releases it, and docker compose down does that for the whole stack — which is why down beats stop when you are switching between projects.
Do I need to publish ports for containers to talk to each other?
No. Containers on the same user-defined or compose network reach each other by service name on the container port directly. Publishing is only for reaching a container from the host or beyond.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor