Docker and Kubernetes

Fix "port is already allocated" in Docker

Something else, often an old container, is already using the port. Find it and stop it or pick another port.

Fill these in once
  1. See what is holding the port
    Terminal
    lsof -i :8080

    If a docker process shows up, it's an old container. Anything else is a normal program.

  2. Find the container using it
    Terminal
    docker ps --filter "publish=8080"
  3. Stop that container
    Terminal
    docker stop $(docker ps -q --filter "publish=8080")

    Then start yours again. Or, to keep both, give yours another outside port, like -p 8081:{{port}}.

Check it worked

  1. Check the port is free
    Terminal
    docker ps --filter "publish=8080"

    The list should be empty.

  1. See what is holding the port
    Terminal · Ubuntu / Debian
    sudo lsof -i :8080

    If a docker process shows up, it's an old container. Anything else is a normal program.

  2. Find the container using it
    Terminal · Ubuntu / Debian
    docker ps --filter "publish=8080"
  3. Stop that container
    Terminal · Ubuntu / Debian
    docker stop $(docker ps -q --filter "publish=8080")

    Then start yours again. Or, to keep both, give yours another outside port, like -p 8081:{{port}}.

Check it worked

  1. Check the port is free
    Terminal · Ubuntu / Debian
    docker ps --filter "publish=8080"

    The list should be empty.

  1. See what is holding the port
    PowerShell
    netstat -ano | findstr :8080

    The last number on each line is the program's ID.

  2. Find the container using it
    PowerShell
    docker ps --filter "publish=8080"
  3. Stop that container
    PowerShell
    docker stop (docker ps -q --filter "publish=8080")

    Then start yours again. Or, to keep both, give yours another outside port, like -p 8081:{{port}}.

Check it worked

  1. Check the port is free
    PowerShell
    docker ps --filter "publish=8080"

    The list should be empty.