Skip to content

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

✗ Broken code
# Container 1: docker run -p 80:80 nginx
# Container 2: docker run -p 80:80 nginx  # port 80 already in use!
✓ Fixed code
# Container 1: docker run -p 80:80 nginx
# Container 2: docker run -p 81:80 nginx  # use different host port

The 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> or lsof -i :<port>

  • List running containers: docker ps and check their port mappings

  • In Kubernetes, check if another Service or Pod is using the same port

  • Use ss -tlnp on Linux to find the process holding the port

How to fix

  • Stop the process or container using the port: docker stop <container> or kill <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

Related Resources

Related Glossary

← Back to language