Docker "Port Is Already Allocated": How to Find and Free the Port
Docker can't publish a container's port because something on your machine already uses it — often another container, a local Postgres or a dev server. How to find what's using the port on Mac, Linux and Windows, stop it, or simply map the container to a different port.
Error response from daemon: driver failed programming external connectivity on endpoint db:
Bind for 0.0.0.0:5432 failed: port is already allocated
When you publish a port with -p 5432:5432 (or ports: in Compose), Docker tries to claim that port on your machine. Only one program can listen on a port at a time. Something already has it. (Ports explained)
Step 1: is it another container?
The most common answer. An old container — from yesterday, from another project — is still running with the same port.
docker ps
Look at the PORTS column for 0.0.0.0:5432->5432/tcp. Stop it:
docker stop <container-name-or-id>
Or, with Compose, from the other project's folder:
docker compose down
Stopped-but-not-removed containers don't hold ports; only running ones do.
Step 2: is it something outside Docker?
A Postgres installed directly on your machine, a dev server, another app. Find the process:
macOS / Linux:
sudo lsof -i :5432
# or
sudo ss -tlnp | grep 5432
Windows (PowerShell):
Get-NetTCPConnection -LocalPort 5432 | Select-Object OwningProcess
Get-Process -Id <PID>
Then stop it — for a local Postgres service:
brew services stop postgresql # macOS with Homebrew
sudo systemctl stop postgresql # Linux
or end the process. (How to kill a process in Linux)
Step 3 (often the best): use a different port
You don't have to fight over the port. Map the container to a different port on your machine:
docker run -p 5433:5432 postgres
# docker-compose.yml
services:
db:
image: postgres:18
ports:
- "5433:5432" # host:container
The left number is your machine's port; the right is the container's. Inside the container Postgres still uses 5432. Your app on the host connects to localhost:5433; other containers in the same Compose project connect to db:5432 and don't need a published port at all. (Docker Compose for local development, Postgres connection strings)
Do you need to publish the port at all?
If only other containers talk to a service (like your database), don't publish it. Containers on the same Compose network reach each other by service name. Publishing ports you don't need also exposes them more widely than you might expect — on a server, a published port can bypass a UFW firewall. (Container networking internals)
To publish only to your own machine:
ports:
- "127.0.0.1:5432:5432"
Still allocated after stopping everything?
- Docker Desktop sometimes keeps a port reserved after a crash — restart Docker Desktop.
- Windows reserves ranges of ports for Hyper-V. Check with
netsh interface ipv4 show excludedportrange protocol=tcpand pick a port outside them. - A container in a restart loop may grab the port intermittently. (Docker container keeps restarting)
Same idea, different message
Outside Docker, the same problem shows up as EADDRINUSE: address already in use :::3000 from Node.js, or "Address already in use" from Python. The fix is the same: find the process, stop it, or use another port.
EasySpawn runs your app, database and services on a server where ports and networking are configured for you — no local port collisions to untangle. See how it works or join the waitlist.
Related: Docker Compose for Local Development · What Is an IP Address and a Port? · Docker Commands Cheat Sheet · ECONNREFUSED 127.0.0.1:5432
Keep reading
"exec format error" in Docker: ARM vs x86 Images Explained
exec format error almost always means a container image built for one CPU architecture (ARM, like Apple Silicon Macs) is running on another (x86/amd64 servers), or vice versa. How to check, build for the right platform with --platform and buildx, and the other cause: scripts without a shebang or with Windows line endings.
Docker "Permission Denied While Trying to Connect to the Docker Daemon Socket": Fix
Your user isn't allowed to talk to Docker's socket at /var/run/docker.sock. The quick fix (add yourself to the docker group), why that's effectively root access, the safer rootless alternative, and the other causes: Docker not running, Docker Desktop, WSL and CI.