How to Fix Docker Port Already in Use Errors (2026)

You run docker compose up -d and instead of a working container you get this:

Error response from daemon: driver failed programming external connectivity on endpoint webserver: Bind for 0.0.0.0:80 failed: port is already allocated

A docker port already in use error means exactly what it says: some other process, on the host or in another container, has already claimed the port your container wants to bind. Only one listener can own a port at a time. The fix takes two minutes once you know where to look: find the process holding the port, then either stop it or give your container a different host port. This guide walks through both, plus the edge cases that make the error come back after you thought you fixed it.

Why You Get a “Docker Port Already in Use” Error

When you publish a port with -p 8080:80 or the ports: block in Compose, Docker asks the host kernel to listen on that port and forward traffic into the container. If anything else is already listening there, the bind fails and Docker aborts before the container even starts. The culprit is almost always one of three things: a host service like Apache or Nginx sitting on port 80 or 443, another container that mapped the same host port, or a container that is restarting and still holding its old binding. The rest of this guide is about figuring out which one it is on your machine.

Step 1: Find What Is Listening on the Port

Start with ss, the tool that ships with every modern Linux server:

sudo ss -tulpn | grep :80

The output tells you the process name and PID. A typical result looks like apache2 with a PID, or docker-proxy with a PID, which points to another container. If you prefer the classic syntax, lsof gives the same answer:

sudo lsof -i :80

Both of these answer the question of how to find the process using a port, and they work whether the culprit is a host service or Docker itself. If the process turns out to be Docker-related, list your running containers to see which one took the port:

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

This table view is faster to read than the default docker ps output when you have a dozen containers. Once you know the name, docker port <container-name> confirms its exact port mapping.

Common Cause 1: A Host Service Already Owns the Port

The most common conflict on a fresh VPS is a web server you installed months ago and forgot about. Apache and Nginx both listen on port 80 (and 443) by default, and that collides with every container you try to expose on those ports. Check what is running:

sudo systemctl status apache2 nginx

You have two options. If the host web server is doing real work, keep it and remap the container to a different host port (shown in the next section). If it is leftover from an earlier setup, stop it and disable it from starting at boot:

sudo systemctl stop apache2
sudo systemctl disable apache2

The same logic applies to any host daemon: MySQL on 3306, a host-installed Prometheus on 9090, anything. If you are still deciding how to manage services on your server, read our breakdown of Docker vs Podman on a VPS before you rip things out, since switching runtimes changes which port conflicts you will see.

Common Cause 2: Another Container Grabbed It First

Docker port conflicts happen between containers too, usually after copy-pasting a docker-compose.yml and forgetting both projects map port 80. The docker ps table from Step 1 shows the overlap immediately. Fix it by stopping the container you do not need, or give one of them a different host port. If the conflicting container was started by a different Compose project, remember that Compose project names share the host port namespace even when their internal networks are isolated.

The Usual Fix: Remap the Port Instead of Fighting for It

In most cases the fastest resolution is to stop competing for the occupied port. Docker port mapping syntax is HOST:CONTAINER, and the host side is yours to choose. Change 80:80 to 8080:80 and the container still serves its app on port 80 internally while the host listens on 8080. In a Compose file:

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

Or on the command line:

docker run -d -p 8080:80 --name webserver nginx:latest

Then confirm the new binding before you touch anything else:

curl -I http://localhost:8080

A 200 OK (or whatever your app returns) proves the remap worked and the container is healthy. If you are new to Compose files, our walkthrough of writing your first docker-compose.yml covers the ports: block in context, and if Docker is not installed yet, follow how to install Docker Compose on Ubuntu 24.04 first so the commands here run as shown.

Edge Cases: TIME_WAIT, Ghost Containers, and Stale Bindings

Sometimes the error persists even when nothing seems to be listening. A container that crashed mid-shutdown can leave a docker-proxy process holding the port. List all containers including stopped ones:

docker ps -a | grep -i exited

Remove the stale ones:

docker container prune -f

Another trap: TCP connections in TIME_WAIT state can block an immediate rebind right after a container stops. Waiting 60 seconds usually clears it. If the port is still held after pruning, restart the Docker daemon as a last resort (this stops all running containers, so do it during a maintenance window):

sudo systemctl restart docker

After the restart, bring your stack back up and re-check with ss -tulpn that each port maps where you expect.

Preventing the Problem: A Simple Port Plan

Port conflicts are a bookkeeping problem more than a technical one. Keep a short list of which host ports your stacks use. A practical convention: 80 and 443 for your reverse proxy only, 3000-range for Node apps, 8000-range for Python apps, 9000-range for admin tools. If you run several Compose projects on one VPS, like the stacks in our guide to self-hosting Nextcloud with Docker Compose, document each project’s ports in a comment at the top of the Compose file. Future you will thank present you the next time a port is already allocated.

FAQ

Can two containers share the same host port? No. One host port maps to exactly one container. Two containers can listen on the same port internally (for example both on port 80 inside their own network namespaces), but their host-side ports must differ.

Does changing the container port fix it too? Rarely what you want. The container port is dictated by the app inside (Nginx serves on 80). Change the host side, which is the part that conflicts.

What if ss shows nothing but Docker still fails? Check docker ps -a for restarting or exited containers holding a stale binding, then prune and, if needed, restart the Docker daemon as described above.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.