How to Fix Docker Port Already in Use Error
In this tutorial, you'll learn about How to Fix Docker Port Already in Use Error. We cover key concepts, practical examples, and best practices.
You run docker run -p 8080:80 and get port is already allocated or address already in use — the host port is occupied by another process or container.
The Problem
docker: Error response from daemon: driver failed programming external connectivity
on endpoint (xxx): Bind for 0.0.0.0:8080 failed: port is already allocated.
Or:
Error starting userland proxy: listen tcp4 0.0.0.0:8080: bind: address already in use
Step-by-Step Fix
Step 1: Find which container is using the port
docker ps --format "table {{.Names}}\t{{.Ports}}" | grep 8080
Step 2: Stop the conflicting container
docker stop <container-name>
Or remove it:
docker rm -f <container-name>
Step 3: Find non-Docker processes on the port
sudo lsof -i :8080
Or:
ss -tlnp | grep 8080
Expected:
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* users:(("nginx",pid=1234,fd=6))
Step 4: Kill the process
sudo kill -9 <PID>
Step 5: Use a different host port
If you cannot free the port, map to a different one:
docker run -p 8081:80 nginx
Step 6: Check for Docker overlay network conflicts
Sometimes Docker overlay networks can occupy host ports. List all Docker networks:
docker network ls
Inspect each network for port usage:
docker network inspect bridge | grep -i subnet
If the Docker bridge subnet overlaps with your host network, change it in /etc/docker/daemon.json:
{
"bip": "10.200.0.1/24"
}
Step 7: Verify the port is free
ss -tln | grep 8080
No output means the port is free.
Prevention Tips
- Use
docker psbefore starting containers to check port usage - Use dynamic port mapping with
-p 80(no host port) to let Docker assign a random port - Stop unused containers with
docker stopordocker rm - Document port allocations for your services
Common Mistakes with port conflict
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world DOCKER code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro