Skip to content

How to Fix Docker Port Already in Use Error

DodaTech Updated 2026-06-24 2 min read

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 ps before 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 stop or docker rm
  • Document port allocations for your services

Common Mistakes with port conflict

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### How do I find which container is bound to a port without grep?

Use docker port <container> to list port mappings for a specific container. For all containers, use docker ps --format "table {{.Names}}\t{{.Ports}}".

Can two containers share the same host port?

No, host ports are exclusive. Use different host ports and let Docker's internal networking route traffic within the container network.

What does docker run -P do?

The -P flag publishes all exposed container ports to random high-numbered host ports (32768-60999). Use docker port <container> to see the mappings.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro