Docker Bind: Address Already in Use — Fix It in 60 Seconds
Docker bind: address already in use error? Find the process hogging the port, kill it safely, and prevent it from ever breaking your deploy again.
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
- ✓Basic familiarity with running Docker containers (docker run, docker ps)
- ✓Access to a terminal on the host machine
- ✓Understanding of what a TCP port is (port 80, 443, 8080)
Run sudo lsof -i :8080 (or sudo ss -tulpn | grep 8080) to find the process holding the port, then either stop that process or change your container's host port mapping with -p 8081:80. If it's a stale container, docker ps -a and docker rm -f clears it.
Imagine a hotel with one phone line per room number. If room 8080 is already occupied by a guest making a call, the front desk can't assign that same line to a new guest — the switchboard physically can't route two calls to one line. Docker is the front desk, the port is the phone line, and the error is the switchboard saying 'that line is busy.' You either evict the current guest or give the new one a different room number.
Every Docker user hits `bind: address already in use` within their first week, and most of them fix it wrong — they reboot the machine. That's like burning down the hotel because one phone line is busy. The error is not a Docker bug. It's the Linux kernel doing exactly what it's supposed to do: enforcing that one port belongs to one listener. The confusion comes from Docker's port publishing layer, which sits between your container and the host network and makes it non-obvious who actually owns the port. Once you see that layer clearly, the fix takes ten seconds. By the end of this article you'll be able to identify the exact process holding any port, distinguish between a stale container and a host service, choose between killing the offender and remapping the port, and set up your compose files so this never bites you in CI again.
What 'Bind' Actually Means (And Why the Kernel Cares)
Before you can fix the error, you need to know what a bind is. When a program wants to receive traffic on a port, it calls the syscall, which tells the kernel: 'route packets arriving on this IP and port to me.' The kernel keeps a table of these claims. Only one process can hold a given (IP, port, protocol) tuple at a time. Docker's bind()-p 8080:80 flag doesn't magically create a new port — it asks the kernel to bind host port 8080 and forward traffic to the container's port 80. If anything else already holds 8080, the kernel returns EADDRINUSE, and Docker surfaces it as bind: address already in use. That's the whole story. No Docker daemon bug, no networking wizardry — just a kernel table with a conflict.
docker-proxy appearing in ss output is normal — it's Docker's userland proxy forwarding traffic. The real conflict is the other line. If you only see docker-proxy, the port is held by a container; if you see a named binary, it's a host process.The Two Flavors of the Error (And How to Tell Them Apart)
Not all address already in use errors are the same. There are two distinct causes, and the fix differs. Flavor one: a host process (nginx, a sidecar, a dev server you forgot) holds the port. Flavor two: a Docker container — often a stale one from a previous docker run — holds the port. The error text is nearly identical, but the diagnostic path diverges. For flavor one, ss shows a real binary name. For flavor two, ss shows docker-proxy and docker ps shows a running container. Mixing these up wastes time: people docker rm -f containers that don't exist while nginx laughs at them from the host.
ss shows docker-proxy AND docker ps shows a container on that portdocker stop <id> or docker rm -f <id>ss shows a named binary (nginx, node, python) and no containersudo systemctl stop <service> or sudo kill <PID>ss shows both docker-proxy and a host binary on the same port0.0.0.0, the other on 127.0.0.1. Stop the host binary or remap the container.The Fastest Fix: Remap the Host Port
When you don't care which host port the container uses — and in dev you usually don't — the fastest fix is to remap. Change -p 8080:80 to -p 8081:80 and move on. The container's internal port stays 80; only the host side changes. This is the right move when the conflicting process is something you need (a local Postgres, a colleague's service, a system daemon). It's the wrong move in production, where the host port is part of your contract with the load balancer. Know which situation you're in before you reach for it.
The Correct Fix: Find and Kill the Offender
In production, the host port is part of your infrastructure contract. You don't remap — you find the process that stole the port and stop it. The sequence is always the same: identify the PID with ss or lsof, confirm what it is with ps, then stop it cleanly. The trap is killing the wrong thing. A docker-proxy PID is a child of the Docker daemon; killing it directly can leave the daemon in a confused state. Kill the container instead. A systemd-managed service should be stopped with systemctl stop, not kill -9, so its socket closes cleanly and doesn't linger in TIME_WAIT.
sudo kill -9 on a docker-proxy PID. It orphans the container's network namespace and you'll need to restart the Docker daemon to recover. Stop the container with docker stop, not the proxy.Stale Containers: The Most Common Beginner Trap
You ran docker run yesterday, forgot about it, and today your new container can't bind. docker ps shows nothing because the old container exited — but a container in Exited state still holds its port mapping until it's removed. This is the single most common cause of the error for beginners. The fix is two commands: docker ps -a to see everything including stopped containers, then docker rm -f <id> to remove the offender. Better: use --rm on every docker run in dev so containers clean themselves up when they exit.
docker ps only shows running containers. If your port is held by an exited container, docker ps shows nothing and you'll waste 20 minutes. Always use docker ps -a when debugging port conflicts.Docker Compose: Why 'down' Isn't Always Enough
With Compose, the error usually appears after a docker compose up following a previous run. docker compose down stops and removes containers, but if a container was started outside Compose (or a docker run snuck in), down won't touch it. The reliable sequence is: docker compose down --remove-orphans to clean up anything Compose knows about, then check docker ps -a for strays. Also watch for docker compose up without -d — it attaches to the terminal, and Ctrl+C sometimes leaves the container running depending on your Compose version.
docker compose up fails with bind error after a previous rundocker compose down --remove-orphans then docker compose up -ddown succeeds but up still failsdocker ps -a — a container started outside Compose is holding the port; docker rm -f itsudo ss -tulpn | grep :<port> and stop the host serviceBinding to 127.0.0.1 vs 0.0.0.0: The Subtle Conflict
Here's a case that confuses even experienced devs. A process bound to 127.0.0.1:8080 and a container trying to bind 0.0.0.0:8080 will conflict — even though they look like different addresses. Why? 0.0.0.0 means 'all interfaces,' which includes 127.0.0.1. The kernel won't let two listeners overlap. The error message will say bind: address already in use on 0.0.0.0:8080, and ss will show the host process on 127.0.0.1:8080. The fix is either to stop the host process or to bind the container to a specific IP: -p 127.0.0.1:8080:8080 — but that only works if the host process is on a different IP, which it usually isn't.
0.0.0.0 is a wildcard, not an address. It means 'every interface on this host,' which includes loopback. That's why a 127.0.0.1 listener blocks a 0.0.0.0 bind — they overlap.Preventing It: Pre-Flight Checks and Port Contracts
The best fix is the one you never have to run. In CI and deploy pipelines, add a pre-flight check that fails fast with a clear message before Docker even tries to bind. In dev, standardize on a port range per project (e.g., 8000–8099 for project A, 8100–8199 for project B) so two projects never collide. In Compose, use 127.0.0.1: prefixes for services that don't need external access — it shrinks the conflict surface and is a security win too. These three habits eliminate 90% of bind errors before they happen.
ports: ["127.0.0.1:8080:8080"] for internal services. It binds only to loopback, so it can't conflict with a 0.0.0.0 listener and it's not exposed to the network — two wins for one line.The Port That Broke the Blue-Green Deploy
bind: address already in use on port 8080, but docker ps showed only the blue container running.docker ps -a.127.0.0.1:8080 because its stop script had a race condition and exited before the socket closed. Docker's port publishing tried to bind 0.0.0.0:8080, which conflicts with any listener on the same port regardless of interface.sudo ss -tulpn | grep :8080 revealed the sidecar PID. sudo systemctl stop checkout-healthcheck && sudo systemctl disable checkout-healthcheck freed the port. The deploy pipeline was patched to run ss -tulpn | grep :8080 as a pre-flight gate and fail fast with a clear message.- Docker only owns the ports it publishes — anything else on the host can steal them, and
docker pswill never show you the thief.
Error response from daemon: driver failed programming external connectivity ... bind: address already in use on deploysudo ss -tulpn | grep :<PORT> to identify the PID and process name. 2. If it's docker-proxy, run docker ps --filter publish=<PORT> to find the container, then docker stop <id>. 3. If it's a named host binary, run sudo systemctl stop <service> or sudo kill <PID>. 4. Re-run the deploy.sudo ss -tulpn | grep :<PORT>. 2. Check systemd units: systemctl list-units --type=service | grep -i <app>. 3. Stop the orphaned sidecar with sudo systemctl stop <unit>. 4. Add a pre-flight ss check to the deploy pipeline.docker compose up fails with bind error after a previous rundocker compose down --remove-orphans. 2. docker ps -a --filter publish=<PORT> to find strays. 3. docker rm -f <id> for any container outside the Compose project. 4. docker compose up -d.0.0.0.0:<port> but ss shows a 127.0.0.1:<port> listenersudo ss -tulpn 'sport = :<PORT>'. 2. Stop the loopback listener (sudo kill <PID> or systemctl stop). 3. If the host process must stay, bind the container to a different host IP: -p <HOST_IP>:<PORT>:<CONTAINER_PORT>.sudo ss -tulpn | grep :8080docker ps -a --filter publish=8080docker rm -f <id>. If host process: sudo kill <PID> or sudo systemctl stop <service>.| File | Command / Code | Purpose |
|---|---|---|
| inspect-port-ownership.sh | sudo ss -tulpn | What 'Bind' Actually Means (And Why the Kernel Cares) |
| classify-port-conflict.sh | PORT=8080 | The Two Flavors of the Error (And How to Tell Them Apart) |
| remap-container-port.sh | docker run -d --name checkout-api -p 8081:80 checkout-api:latest | The Fastest Fix |
| kill-port-offender.sh | PORT=8080 | The Correct Fix |
| clean-stale-containers.sh | docker ps -a --format 'table {{.ID}}\t{{.Names}}\t{{.Status}}\t{{.Ports}}' | Stale Containers |
| docker-compose.yml | services: | Docker Compose |
| diagnose-ip-conflict.sh | sudo ss -tulpn 'sport = :8080' | Binding to 127.0.0.1 vs 0.0.0.0 |
| preflight-port-check.sh | set -euo pipefail | Preventing It |
Key takeaways
docker ps only shows running containersdocker ps -a when debugging port conflicts, because exited containers still hold their port mappings.0.0.0.0 is a wildcard that includes 127.0.0.1, so a loopback listener blocks a wildcard bindss -tulpn | grep :<port> check in your deploy pipeline that fails fast with a clear message before Docker even tries to bind.Interview Questions on This Topic
Why does a process bound to `127.0.0.1:8080` block a Docker container trying to bind `0.0.0.0:8080`?
0.0.0.0 is a wildcard meaning 'all interfaces,' which includes loopback. The kernel treats the two binds as overlapping, so the second one fails with EADDRINUSE. The fix is to stop the loopback listener or bind the container to a specific non-loopback IP.Frequently Asked Questions
Keep going
20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.
That's Docker. Mark it forged?
3 min read · try the examples if you haven't
