Skip to content

Docker error: port is already in use, how to free it

Medium 10 min Updated:

Quick answer

The error "port is already allocated" means some process is already using the requested port. Find the process using `netstat` or `lsof`, stop it, or change the port mapping in the Docker configuration.

How to recognize the problem

  • Docker shows the error `Bind for 0.0.0.0:8080 failed: port is already allocated`
  • The container does not start even though the image is correct
  • `docker ps` does not show a container using the port, but the error persists
  • The port is still in use after restarting the Docker service

Most common causes

Another container is using the same port

The most common cause is a running container that already maps the same port on the host. Check `docker ps` for a container with the same port.

A local application is holding the port

Some process on the host (e.g., a development server, a database) may be occupying the port. It could be an application you forgot to close.

A previous container was not stopped properly

If a container was forcibly removed or the Docker process ended abnormally, a leftover process may still hold the port. Sometimes you need to manually release resources.

Another service on the host is using the same port

Another service on the host may be holding the port Docker is trying to use (e.g., IIS, Apache). Check whether the port is already in use before starting the container.

Step-by-step fix

  1. 1

    Check which containers are using the port

    Run docker ps to see the list of active containers and their mapped ports. If you find a container using the same port, stop it with docker stop <name_or_id>. Then try starting your container again.

  2. 2

    Find the process on the host holding the port

    On Linux, use sudo lsof -i :<port> or sudo netstat -tulpn | grep <port>, and on macOS use sudo lsof -i :<port>. On Windows, open PowerShell as administrator and run netstat -ano | findstr :<port>. Note the process PID.

  3. 3

    Stop the process occupying the port

    On Linux/macOS: kill <PID> (or kill -9 <PID> if it does not respond). On Windows: taskkill /PID <PID> /F. Be careful: do not stop processes you do not recognize; stopping system processes can affect other applications.

  4. 4

    Change the port mapping in the Docker configuration

    If you cannot free the port, change the mapping in the docker run command or in docker-compose.yml. For example, instead of -p 8080:80 use -p 8081:80. Then start the container again.

  5. 5

    Clean up leftover containers and networks

    Sometimes leftover containers are the cause. Run docker ps -a to see all containers, then remove unnecessary, previously stopped ones with docker rm <name_or_id>. You can also use docker system prune to remove stopped containers, unused networks, dangling images, and cache. Caution: this command permanently deletes those resources, so check what you need and back up data before running it.

  6. 6

    Restart the Docker service

    If the port still appears to be in use, restart Docker: on Linux sudo systemctl restart docker, on Windows restart Docker Desktop. Then check whether the port is free.

  7. 7

    Verify that the port is free

    Run netstat or lsof again to confirm the port is no longer in use. Then start your container with docker run or docker compose up.

If nothing helps

If the port remains in use despite all the steps, check whether a service on the host automatically occupies the port (e.g., antivirus, VPN, or another Docker container). You can temporarily disable those services. Also, on Linux sudo netstat -tulpn shows both IPv4 and IPv6 connections, so check that the conflict is not only on one of them. In extreme cases, change the port the container binds to (e.g., to 8081) and update the application configuration. If that also fails, consider restarting the computer.

How to prevent it

  • Periodically clean up stopped containers with `docker container prune`, but first check that you do not need them.
  • Use Docker Compose (`docker compose`) and define unique ports for each service.
  • Before starting a container, check that the port is free using `netstat` or `lsof`.
  • Avoid running multiple instances of the same application on the same port.

When to call a professional

If you cannot find the process holding the port or suspect a deeper Docker networking issue, contact IT support. They can safely inspect system processes and configuration.

Software and product development · Custom apps, SaaS and an MVP in 4 to 8 weeks

FAQ

How do I know which process is using a port on Windows?

Open PowerShell as administrator and run `netstat -ano | findstr :<port>`. The last column is the PID. Then in Task Manager, on the Details tab, look for the process with that PID.

Is it safe to use `docker system prune`?

`docker system prune` removes all stopped containers, unused networks, dangling images, and build cache. The `-a` option also removes all unused images. This can free up space, but be careful not to delete something you need. Before running it, check the list of containers and disk usage.

Why does Docker report an error even though there are no active containers?

A process on the host or a leftover Docker process may have taken the port. Check `netstat` or `lsof` to find the real cause, and use `docker ps -a` to rule out stopped containers.

Share
Didn't find a fix?The AI IT assistant is available 24/7. Describe the problem or send a screenshot of the error and get instructions for your case.Ask the AI IT assistant