Port already in use: container cannot bind
Quick summary
Your container cannot bind to a port — another process or container is already using it.
Why this happens
Another container or process on the host is already listening on the same port
A previous container instance was not stopped and is still holding the port
The port mapping in docker-compose or run command conflicts with another service
Minimal example
# docker-compose.yml
services:
web:
ports:
- '80:80'
api:
ports:
- '80:8080' # conflict — port 80 already used by web# docker-compose.yml
services:
web:
ports:
- '80:80'
api:
ports:
- '8080:8080' # different host port — no conflictMapping the api service to a different host port (8080) avoids the conflict with the web service on port 80.
How to diagnose
Check which process is using the port: netstat -tulpn | grep :PORT or lsof -i :PORT
List running containers and their port mappings with docker ps
Verify the port mapping in the docker-compose or run command for conflicts
How to fix
Stop the process or container that is using the port
Change the container's port mapping to use a different host port
Use Docker networking to let containers communicate without host port binding
How to prevent
Use a port management strategy or dynamic port allocation to avoid conflicts
Document port assignments and use docker-compose to manage port mappings centrally
Related Resources
Related Glossary
Related Lessons
- Container Networking
Learn about ports.