Docker "Bind for 0.0.0.0:80 failed: port is already allocated"
Docker "port is already allocated" means another container publishes that host port. Find it with docker ps --filter publish and stop it or move the port.
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint …: Bind for 127.0.0.1:18081 failed: port is already allocatedAnother container already holds the host port you asked for with -p. Find it with docker ps --filter publish=<port> and stop it, or give the new container a different host port. If you published without an address, as in -p 80:80, the message shows 0.0.0.0:80 in that spot.
When a host process (nginx, Apache, and so on) holds the port instead of Docker, the message is different. Tell the two apart first.
| Message ends with | Port held by | Go to |
|---|---|---|
Bind for …: port is already allocated | another container | step 1 |
failed to bind host port …/tcp: address already in use | a host process | step 3 |
[1] Find the container that publishes the port. Copy the port number from the error.
$ docker ps --filter publish=18081 --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'NAMES STATUS PORTSweb Up Less than a second 127.0.0.1:18081->80/tcpdocker ps lists running containers only by default. Stopped containers do not hold ports, so if nothing shows here, go to step 3. For Compose, show the project too:
$ docker ps --filter publish=18081 --format '{{.Names}} {{.Label "com.docker.compose.project"}}'A common leftover is a Compose project started from an old directory name. Run docker compose down in that project's directory.
[2] Pick one:
- If the old container is not needed, stop it:
docker stop web(anddocker rm webto delete it). If it serves traffic, check what it is first. - If you need both, change only the host side of the new container's port. In
-p 127.0.0.1:18082:80the number before the last colon is the host port;80after it is the port inside the container and stays.
[3] If no container shows up, see what listens on the host. -p shows process names, so run it with sudo.
$ sudo ss -ltnp 'sport = :18082'State Recv-Q Send-Q Local Address:Port Peer Address:PortProcessLISTEN 0 5 127.0.0.1:18082 0.0.0.0:* users:(("python3",pid=…,fd=3))In that case docker run fails like this:
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint …: failed to bind host port 127.0.0.1:18082/tcp: address already in useIf the process is a host service such as nginx or apache2, decide whether to stop it or move the container's port. The host-side checks are the same as in nginx bind() Address already in use. If the process is docker-proxy, a container holds it:
LISTEN 0 4096 127.0.0.1:18081 0.0.0.0:* users:(("docker-proxy",pid=…,fd=8))If step 1 showed no container, the CLI may be talking to a different daemon. Check the current context with docker context ls.
[4] Remove the container the failed docker run left behind. A run that fails on the port still creates the container, in Created state:
$ docker ps -a --filter name=web-2 --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'NAMES STATUS PORTSweb-2 CreatedRun the same docker run again with that name and you get a name conflict instead:
docker: Error response from daemon: Conflict. The container name "/…-2" is already in use by container "6321994a…". You have to remove (or rename) that container to be able to reuse that name.Delete it with docker rm web-2, then run again with the port you chose in step 2. In scripts that run repeatedly, use docker run --rm or a docker rm -f before the run.