Docker — Port Is Already Allocated
Resolve Docker bind failures by identifying which container or host process already publishes the port, then changing or freeing the mapping without disrupting unrelated services.
Symptoms
- docker: Error response from daemon: Bind for 0.0.0.0:8080 failed: port is already allocated
- A container starts and immediately exits while another service still serves the old port
- docker compose up fails after a previous stack was not shut down
Possible causes
- Another running container publishes the same host port
- A host process such as a local dev server listens on the port
- A stopped container still holds a network resource or a duplicate compose project is running
Diagnosis steps
List container port mappings
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'Interpretation
Look for the host port on the right side of the mapping. The container name identifies the owner.
Inspect a specific container mapping
docker port <container-name>Interpretation
Shows the exact host binding, including 0.0.0.0 versus 127.0.0.1.
Check whether a host process owns the port instead
lsof -i :8080 # Windows: netstat -ano | findstr :8080Interpretation
If no container appears, a local process is listening. Stop it only after confirming what it is.
Root cause
Docker publishes a container port by binding a host socket. The host allows only one listener per address and port, so a second container or process cannot bind the same host port while the first mapping exists.
Fix
Stop the container that owns the port if it is yours, or change the host side of the mapping, for example -p 8081:80. In Compose, remove the old project with docker compose down and give each stack a distinct project name or host port.
Risk note
Do not stop containers you do not own on a shared host. Removing a stack with volumes can delete data; check the compose file and use down without -v unless you intend to remove volumes.
Verify the fix
Start the container again and confirm the new mapping with docker ps. Curl the mapped host port and verify the response comes from the intended container. Restart the stack once to confirm the mapping is released correctly.
A host port can have one owner
Publishing a container port is a host-level bind. -p 8080:80 means the host listens on 8080 and forwards to container port 80. If another container or process already listens on 8080, Docker cannot create the mapping.
docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker port my-app
lsof -i :8080
Fix
If the old container is yours, stop and remove it:
docker stop my-old-app
docker rm my-old-app
If both services are needed, change only the host port:
docker run -p 8081:80 my-app
In Compose, set distinct host ports or distinct project names so two checkouts do not compete for the same binding.
Verify
docker psshows the expected host-to-container mapping.- A request to the host port reaches the intended container.
- The old container is no longer running or no longer publishes the port.
- A restart of the stack succeeds without manual cleanup.
If the port belongs to an unknown process, choose another host port rather than terminating a service you do not own.