docker network_error ai_generated true

Bind for 0.0.0.0:PORT failed: port is already allocated

ID: docker/port-already-allocated

Also available as: JSON · Markdown
92%Fix Rate
94%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
24 active

Root Cause

The host port is already in use by another container or process. Cannot bind two services to the same port.

generic

Workarounds

  1. 92% success Find and stop the process or container using the port
    # Find what's using the port:
    lsof -i :8080
    # or:
    ss -tlnp | grep 8080
    
    # If it's a Docker container:
    docker ps --filter 'publish=8080'
    docker stop <container_id>
    
    # If it's a host process:
    kill <pid>

    Sources: https://docs.docker.com/network/

  2. 88% success Use Docker Compose to manage port allocation conflicts
    # In docker-compose.yml, map to a different host port:
    services:
      web:
        ports:
          - '8081:8080'  # host:container
    
    # Use 'docker compose down' before 'docker compose up' to ensure old containers release ports.
    # Use 'docker compose up --force-recreate' if containers are stuck.

    Sources: https://docs.docker.com/compose/networking/

  3. 85% success Bind to 0.0.0.0 and let Docker assign a random host port
    # Use -P to publish all exposed ports to random host ports:
    docker run -P myimage
    
    # Or specify only the container port (random host port assigned):
    docker run -p 8080 myimage
    
    # Find the assigned port:
    docker port <container_name> 8080

    Sources: https://docs.docker.com/network/

Dead Ends

Common approaches that don't work:

  1. Killing the process using the port without checking what it is 60% fail

    May kill a critical service; always identify the process first

Error Chain

Frequently confused with: