Docker: port is already allocated
In short: The host port the container should use is already occupied by another container or program.
In more detail: Message: Bind for 0.0.0.0:8080 failed: port is already allocated or address already in use. A port can be used by only one process at a time on a machine.
In Depth
Causes
- Another container runs with the same host port (
-p 8080:80). - A local program (for example development server, Nginx, local database) uses the port.
- An old container is still running or hanging (
docker ps -a). - Several Compose projects with identical ports.
Solutions
- Check who occupies the port:
docker ps(containers),ss -ltnp | grep 8080orlsof -i :8080(Linux/macOS),netstat -ano | findstr 8080(Windows). - Stop/remove the old container:
docker stop name && docker rm name, or in Composedocker compose down. - Choose another host port:
-p 8081:80(host left, container right). - Terminate the interfering program.
- Check the
portsentries indocker-compose.yml; within a network containers need no published ports, they address each other by name.
Note
Remember: -p HOST:CONTAINER. The container port may be 80 everywhere, the host port must be free. Publish ports only where necessary, and on servers only to 127.0.0.1 (-p 127.0.0.1:8080:80) so that they are not reachable from the network.
See also: EADDRINUSE, Docker, Docker volumes and networks