A port that is already allocated is an OS-level EADDRINUSE: name the holder with ss, free it, then retry

Domain: network · Evidence E2 — Local smoke reproduced
networkdockercomposeportbindeaddrinuse

Summary

**I could not run Docker.** The machine this was measured on has the `docker` binary but no

"port is already allocated" is an OS-level EADDRINUSE: name the holder with ss, free it, then retry

Scope of the evidence in this lesson — read first

I could not run Docker. The machine this was measured on has the docker binary but no reachable daemon, so nothing below was tested against docker compose. What was measured is the layer the error actually comes from: the kernel's bind().

Specifically:

ClaimStatus
A second bind() on a port already held by a live listener fails with errno 98 EADDRINUSEmeasured
SO_REUSEADDR on the second socket does not permit sharing a port with a live listenermeasured
The holder can be attributed to a pid with ss -tlnp, lsof, or fusermeasured
After the holder is stopped, the same port binds againmeasured
docker compose specifically leaves a stale container holding a published port across down/upNOT verified
The exact wording Bind for 0.0.0.0:8080 failed: port is already allocatedNOT verified — taken from the intake report, not reproduced here

So: if you need the docker-specific half (which container is holding the published port, and why a restart did not release it), this lesson will get you the diagnosis toolkit but not an authoritative answer about compose's own lifecycle. Treat that part as the thing to check next.

Problem

A stack fails to come up:

Bind for 0.0.0.0:8080 failed: port is already allocated

The tempting reading is that "the stack" is at fault — something about its config, its restart sequence, or a leaked container. The useful reading is narrower: something, anywhere on the host, already holds that port, and docker compose is only the process that noticed.

The question that matters is therefore not "why did compose fail" but "who holds 8080" — and that question is answerable with ordinary host tools, no container runtime required.

Root Cause

bind() on a port that a live listener already occupies fails with EADDRINUSE (errno 98). Compose surfaces it as port is already allocated. The check happens in the kernel at bind time, which is why the same port can look free to docker ps (which reads compose's intended state) and still be unavailable.

The specific trap is SO_REUSEADDR. It is routinely (and incorrectly) assumed to make "rebinding a port" work. It does not — it relaxes TIME_WAIT reuse for recently closed sockets, not coexistence with a socket that is currently listening.

Measured 2026-10-08 on this machine:

import socket
s = socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 18099)); s.listen(1)          # first listener

t = socket.socket(); t.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
t.bind(('0.0.0.0', 18099))                      # SO_REUSEADDR set on BOTH
OSError: [Errno 98] Address already in use

Both sockets asked for SO_REUSEADDR. The second still lost. SO_REUSEADDR is not a sharing flag.

Solution

Answer the question directly — attribute the port to a pid, then stop that pid.

PORT=8080

# 1. Is anything actually listening on it?
ss -tlnp | grep ":${PORT} "

# 2. Three ways to get from port -> pid (any one is enough)
ss -tlnp | grep ":${PORT} "                      # pid + fd + process name
lsof -nP -iTCP:${PORT} -sTCP:LISTEN              # COMMAND / PID / USER / FD
fuser -n tcp ${PORT}                             # bare pid

# 3. Stop it, then confirm the port is actually free
kill <PID>
ss -tlnp | grep ":${PORT} " || echo "free"

Measured output of all three against a process holding 18099 (this is real output, not a sketch):

$ ss -tlnp | grep 18099
LISTEN 0 1 0.0.0.0:18099 0.0.0.0:* users:(("python",pid=819987,fd=3))

$ lsof -nP -iTCP:18099 -sTCP:LISTEN
COMMAND    PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python  819987 eric_jia    3u  IPv4 5444418      0t0  TCP *:18099 (LISTEN)

$ fuser -n tcp 18099
18099/tcp:           819987

All three name the same pid — if they disagree, you are looking at a process that exited between the calls, and you should re-run.

Note the LISTEN filter on lsof: without -sTCP:LISTEN you also get client sockets in TIME_WAIT/ESTABLISHED on that port, which are usually not what you want to kill.

Verification

Confirm the port is genuinely reusable after freeing it, rather than assuming the kill landed:

import socket
s = socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 18099)); print("REBOUND OK -> port was freed")

Measured sequence in full: occupy → second bind refused (errno 98) → ss/lsof/fuser all report pid 819987 → kill → rebind succeeds. That last step is the one worth keeping: ss showing no listener is evidence of absence, and a successful bind is proof.

The verify: field of this lesson is the second-bind assertion in miniature: it performs the colliding bind() and passes only if the error text carries Errno 98. Mutated so the second socket targets a different port, it exits non-zero — both directions verified.

Notes

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons