Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Docker: Ports are not available: exposing port TCP address already in use

The daemon could not bind the host port. Find the listener, change the host side of the mapping and stop stale containers or proxies.

~5 min readDocker
#docker#networking#ports#compose
Bold white headline reading PORT UNAVAILABLE with a Docker container crate blocked at an amber port gate held by a cyan rival listener.

Introduction & Quick Summary

A container that ran yesterday refuses to start today:

code
docker: Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:8080 -> 127.0.0.1:0: listen tcp 0.0.0.0:8080: bind: address already in use.

Quick answer: something on the host already owns port 8080. Find it, then either remove it or publish on a different host port.

ss -ltnp 'sport = :8080' # Linux lsof -nP -iTCP:8080 -sTCP:LISTEN # macOS docker ps --format '{{.Names}}\t{{.Ports}}' | grep 8080 docker run --rm -p 8081:8080 myapp # different host port, same container port

The wording is precise: exposing port TCP 0.0.0.0:8080 is the host side that failed, and -> 127.0.0.1:0 says Docker had not even chosen a container port yet. The daemon tried to bind() on the host and the kernel answered EADDRINUSE. Nothing about the image is wrong, which is why rebuilding it changes nothing.

Core Mechanics, Signatures & Reproducible Setup

Publishing a port creates a host-side listener, usually a small docker-proxy process plus a NAT rule. Two listeners may not share the same protocol, address and port unless SO_REUSEPORT is set, which Docker does not do for published ports. Reproduce the collision in two commands:

python3 -m http.server 8080 & docker run --rm -p 8080:80 nginx # Error response from daemon: Ports are not available ...

The host port is the left side of the mapping. -p 8081:80 means "listen on host 8081, forward to container 80". Changing only the host side is enough to keep the container's internal configuration untouched, so an app that hardcodes 8080 inside the image still works.

The same rule applies in Compose, where the string is the mapping:

services: api: build: . ports: - "8081:8080" # host:container - "127.0.0.1:9229:9229"

expose: documents a port for the network but never binds one on the host, so expose cannot cause this error. network_mode: host skips publishing entirely and lets the container bind directly on the host, which produces a different but related failure.

Key Comparison Table

The exact message tells you which layer is stuck:

Message fragmentLayerMeaning
Ports are not available: exposing port TCPDocker daemonhost bind failed
bind: address already in usekernelsome process holds the port
bind: permission deniedkernelprivileged port without rights
bind: cannot assign requested addresskernelhost IP does not exist on this machine
port is already allocatedDocker networkanother container claimed it
listen tcp4 0.0.0.0:80: bind: address already in useDocker, older wordingsame as the first row

And where a published port can be reached from, which decides whether your fix is even visible to the client:

Published addressHost loopbackLANOther containers
-p 8080:80yesyesno
-p 127.0.0.1:8080:80yesnono
-p 0.0.0.0:8080:80yesyesno
-p [::]:8080:80yes, IPv6yesno
no -p, same user networknonoyes, via service name

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Identify the holder before killing anything

bash
sudo ss -ltnp | grep ':8080' # LISTEN 0 4096 0.0.0.0:8080 users:(("docker-proxy",pid=41233,fd=4)) docker ps --filter "publish=8080" --format '{{.ID}} {{.Names}} {{.Status}}'

A docker-proxy holder means another container owns the port; docker ps --filter publish=8080 names it. A java or node holder means a local process is running outside Docker — often a dev server you started last week. Stopping the container is safer than killing docker-proxy, because the proxy is restarted by the daemon while a killed proxy can leave a half-configured NAT rule.

The three publishing styles behave differently when the port is contested:

StyleHost listenerContest behaviourUse when
-p 8080:800.0.0.0:8080 and [::]:8080fails hard on any collisiona service must be on the host
--network hostthe app binds directlythe app's own bind errorLinux only, no NAT wanted
no publish, same networknonenever collidesservice-to-service traffic

2. Stale containers and leftover proxies

bash
docker compose down # removes containers and the network docker compose up -d

A container started with --restart=always comes back automatically after a reboot and grabs the port before your new compose project starts. docker ps -a --filter status=exited reveals the old instance. When the port is held but no container appears in docker ps, restart the daemon (sudo systemctl restart docker) to clear orphaned docker-proxy processes from an interrupted upgrade.

3. Changing the host port without breaking the app

Inside the container the app still listens on 8080, and health checks, reverse proxies and browser links must use the host port instead:

services: api: ports: ["8081:8080"] healthcheck: test: ["CMD", "wget", "-qO-", "http://localhost:8080/healthz"] proxy: image: nginx depends_on: [api] ports: ["80:80"]

The health check runs inside the container, so it keeps 8080. The proxy reaches api:8080 over the compose network by service name, not through the host port. Only the browser-facing URL changes, which is the one place to update documentation. If several services collide, a mapping table in the compose file beats hunting ports one by one; the Docker Compose builder validates the file before you restart the stack.

4. Windows, WSL and the reserved port range

On Windows with Hyper-V or WSL2, large ranges are excluded from dynamic allocation:

powershell
netsh int ipv4 show excludedportrange protocol=tcp net stop winnat net start winnat

If a port falls inside an excluded range, Docker cannot bind it even though nothing is listening. After stopping and starting WinNAT the exclusions are rebuilt and the port usually becomes free. The same symptom appears when two WSL distributions each try to publish the same port, because both bridge through the same host network stack.

5. Addresses that look free but are not

A listener on 127.0.0.1:8080 blocks 0.0.0.0:8080, because 0.0.0.0 covers every address including loopback. The reverse is also true: a container publishing on 0.0.0.0 blocks a local dev server that wants 127.0.0.1. IPv6 adds a third case — :::8080 may or may not collide with 0.0.0.0:8080 depending on net.ipv6.bindv6only, which is why the same compose file behaves differently on two hosts. When the address itself is in doubt, ip addr plus the CIDR calculator settles which interface a binding refers to.

6. Container-to-container traffic mistaken for a port problem

The other half of port troubleshooting is the opposite mistake: the port publishes fine and the client still fails because it used the wrong name.

text
# from another container: wrong curl http://localhost:8080/healthz # from another container: right curl http://api:8080/healthz

localhost inside a container is that container. Publishing a port does not make it reachable inside the Docker network, and DNS resolution by service name only works on a user-defined network — the default bridge network has no DNS for container names. Verify the path from the host with the cURL builder, then repeat the same request from inside a container attached to the same network before changing any mapping. One more check is worth the ten seconds: docker port api prints the actual mapping the daemon installed, which is authoritative when a stale compose file and a fresh docker run disagree.

Common Compilation/Runtime Mistakes & Version Gotchas

Rebuilding the image. The image is not involved in a host bind failure. Rebuilds only waste minutes.

Killing docker-proxy by hand. The daemon restarts it, and the port stays taken; the underlying container is what needs to stop.

Publishing a port range accidentally. -p 8000-9000:8000-9000 claims a thousand ports and produces collisions that look random. Publish only what the host must reach.

Assuming EXPOSE 8080 publishes the port. It is documentation for image users and is ignored by docker run unless -P is used.

Version notes. Docker 20.10 changed the error text to "Ports are not available" and included both sides of the mapping, which made diagnosis much easier than the older bare listen tcp4 message. Docker Desktop 4.x on macOS moved port forwarding to its own vpnkit-successor, so a port may report as busy after an unclean shutdown until Docker Desktop restarts. On Linux, enabling userland-proxy: false in daemon.json removes docker-proxy processes and relies on iptables alone, which frees ports that previously appeared as proxy-held but can change which address is reachable.

A reliable habit. Before adding a service to a compose file, check the host port once: ss -ltn | grep ':8080'. It takes a second and replaces the entire diagnosis above.

Reference material: Docker Engine networking and docker container run port publishing.

Official references

primary documentation

The Docker documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.