Fix: port is already allocated
docker run or docker compose up fails with Bind for 0.0.0.0:8080 failed: port is already allocated. Find what holds the port, another container or a host process, then stop it or publish a different port.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
Starting a container that publishes a port fails immediately with one of these messages:
docker: Error response from daemon: driver failed programming external connectivity on endpoint web (3f1c...): Bind for 0.0.0.0:8080 failed: port is already allocated.
Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:5432 -> 127.0.0.1:0: listen tcp 0.0.0.0:5432: bind: address already in use
Why it happens
-p 8080:80 asks Docker to listen on port 8080 of the host and forward it into the container. Only one program can listen on a given address and port. Either another container already publishes that port, or a normal process on the host (a local PostgreSQL, a development server, another Docker Desktop VM) is using it. The container itself is fine; it was created but could not start.
Step 1: Find out what holds the port
First check other containers, because this is the usual cause after a previous run that was never removed:
docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' | grep 8080
If no container shows up, look for a host process. On Linux:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo ss -ltnp | grep ':8080'
sudo lsof -iTCP:8080 -sTCP:LISTEN
On macOS:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo lsof -iTCP:8080 -sTCP:LISTEN -n -P
On Windows (PowerShell):
Get-NetTCPConnection -LocalPort 8080 -State Listen | Select-Object OwningProcess
Get-Process -Id (Get-NetTCPConnection -LocalPort 8080 -State Listen).OwningProcess
Step 2: Free the port or pick another one
- If the holder is an old container you no longer need:
docker rm -f oldname. With Compose,docker compose downin the other project’s directory. - If it is a host service you want to keep (for example PostgreSQL on 5432), publish the container on a different host port:
-p 5433:5432. The container still listens on 5432 inside; only the host side changes. - If it is a host service you do not need: stop it, for example
sudo systemctl stop postgresqlon Linux. Careful: stopping a service that other people rely on will interrupt them. - On Windows, ports can also be reserved by Hyper-V or WSL even when nothing listens.
netsh interface ipv4 show excludedportrange protocol=tcplists those ranges; choosing a port outside them is the simplest fix.
Step 3: Start the container again
The failed container may still exist in the Created state. Remove it before retrying, or simply run docker compose up -d again, which recreates it:
docker rm web
docker run -d --name web -p 8081:80 nginx:1.27
docker ps
Avoid it next time
- Give containers names and use
--rmfor throwaway runs so stale containers do not pile up. - Bind development services to localhost only,
-p 127.0.0.1:8080:80, which also keeps them off the network. - In Compose, keep host ports in the
.envfile ("${APP_PORT:-8000}:8000") so each developer can change them without editing the YAML.
Official documentation
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0061 Fix: Address already in use (free a busy port) Your server cannot start because another process already listens on its port. This script shows which process holds the port and stops… Intermediate · Fixes an error · Linux, macOS Script Processes & ports · Shell scripting 15 Oct 2026
- 0289 What is a process? A process is a running program; the operating system gives each one its own memory and an ID number. Beginner · Explains an idea · Any system Article Processes & ports 15 Oct 2026
- 0320 Fix: address already in use (EADDRINUSE) Something is already listening on the port your program wants. Find the process with lsof or ss, stop it or pick another… Beginner · Fixes an error · Linux, macOS Article Networking & firewalls · Node.js · Processes & ports 15 Oct 2026
- 0326 Fix: too many open files A process hit its file descriptor limit, usually 1024. Raise it for the shell, the systemd unit or the user, fix a… Intermediate · Fixes an error · Linux, macOS Article Performance & tuning · Processes & ports · Services & systemd 15 Oct 2026
- 0333 Fix: permission denied while trying to connect to the Docker daemon socket Every docker command fails with permission denied on /var/run/docker.sock. The daemon is fine; your user is not allowed to use its socket.… Beginner · Fixes an error · Linux Article Docker basics · Docker fixes 15 Oct 2026
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +2,965 chars