Port Conflict
Error message
Error: bind: address already in use
Quick summary
Two containers or processes are trying to use the same port, causing a binding conflict.
Why this happens
Another container or process is already using the same port on the host
Multiple containers in a Pod are trying to bind the same container port
A previous container did not shut down properly and is still holding the port
The host machine has a service running on the port you are trying to map
Minimal example
# Container 1: docker run -p 80:80 nginx
# Container 2: docker run -p 80:80 nginx # port 80 already in use!# Container 1: docker run -p 80:80 nginx
# Container 2: docker run -p 81:80 nginx # use different host portThe second container tries to map to host port 80 which is already taken. The fix maps it to port 81 instead.
How to diagnose
Check what is using the port:
netstat -tlnp | grep <port>orlsof -i :<port>List running containers:
docker psand check their port mappingsIn Kubernetes, check if another Service or Pod is using the same port
Use
ss -tlnpon Linux to find the process holding the port
How to fix
Stop the process or container using the port:
docker stop <container>orkill <pid>Map the container to a different host port:
docker run -p 8081:80 ...Use a different container port if possible
Restart the host machine if a zombie process is holding the port
How to prevent
Use a port management strategy to avoid conflicts in development
Use Kubernetes Services to abstract port assignments
Document which ports each service uses