Skip to content
port is already allocated

port is already allocated

DodaTech 2 min read

The “port is already allocated” error occurs when Docker tries to bind a container port to a host port that is already in use. Docker cannot share a host port between multiple processes. You must free the port or use a different one.

What It Means

Docker attempted to publish a container port to a host port (e.g., -p 8080:80) but that host port is already bound. The conflict can come from another Docker container, a system service, or any other process.

Why It Happens

  • Another Docker container is already using the same host port mapping.
  • A non-Docker process (e.g., nginx, Apache, a dev server) is listening on that port.
  • A previously stopped container still holds the port binding.
  • Docker did not release the port after a crash or improper shutdown.

How to Fix It

1. List running containers and their ports

docker ps --format "table {{.Names}}\t{{.Ports}}"

Stop any container that is using the conflicting port:

docker stop <container-name>

2. Find which process owns the port

sudo lsof -i :8080

Or using ss:

sudo ss -tlnp | grep :8080

Kill the process if it is safe to do so:

sudo kill -9 <PID>

3. Use a different host port

Re-run your container with an alternate port mapping:

docker run -p 8081:80 nginx

Use -p HOST_PORT:CONTAINER_PORT where HOST_PORT is unique.

4. Remove stopped containers holding the port

docker rm -f $(docker ps -a -q --filter "status=exited")

5. Restart Docker (last resort)

sudo systemctl restart docker

FAQs

Does Docker support dynamic port allocation?
Yes. Use -p 80 (no host port) to assign a random ephemeral host port. Check which port was assigned with docker ps or docker port <container>.
Can I have two containers mapping to the same host port?
No. A host port can only be bound by one process at a time. If you need both containers accessible, map them to different host ports or use a reverse proxy like nginx.
I stopped the container but the port is still allocated. Why?
The container may still be in a “removing” state, or the Docker proxy process (docker-proxy) has not released the port yet. Wait a few seconds or run sudo systemctl restart docker to force cleanup.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro