Chapter 5 · Lesson 5 of 10
Fix it

Fix port already in use (address already in use)

Port already in use means another process or container is already listening on that port, so find the owner with ss or lsof and stop it or pick a different port.

By the end you will

Find the process or container holding a port with ss and lsof, then free it or move your service to another port.

Time
10 min
Level
Beginner
Check
4 questions
You need
  • SSH access to the server with sudo

Your service refuses to start. The log says bind: address already in use, or Docker prints Bind for 0.0.0.0:8080 failed: port is already allocated. Both mean the same thing: something else already listens on that port. You need to find the owner, then decide whether to stop it, remove a leftover, or move your service to a different port.

How it works#

A listening socket is identified by an address and a port, for example 0.0.0.0:8080. The kernel allows one listener per address and port, so a second program that asks for the same pair is refused. The error appears in the program’s own words: Go says listen tcp :8080: bind: address already in use, Node says EADDRINUSE, nginx says bind() to 0.0.0.0:80 failed (98: Address already in use).

When you publish a container port with -p 8080:3000, Docker listens on the host port 8080 and forwards to port 3000 inside. Only the host side can clash. Two containers can both use 3000 inside as long as they publish different host ports.

Step 1: Find the listener#

bash
sudo ss -ltnp

The flags are l for listening, t for TCP, n for numbers, p for process. Without sudo you only see your own processes. Find the line with your port in the Local Address:Port column and read the Process column. The lesson find what is using a port goes deeper. To look at one port only:

bash
sudo lsof -i :8080

lsof prints the command, PID and user. If you see docker-proxy, the port is published by a container and you need Step 2. If you see nginx or apache2 on port 80 or 443, a web server on the host holds it.

Step 2: If it is Docker, find the container#

bash
docker ps --format 'table {{.Names}}\t{{.Ports}}\t{{.Status}}'

Look for the host port in the Ports column, such as 0.0.0.0:8080->3000/tcp. Containers that are not running but are still created do not hold ports, so docker ps -a is rarely needed for this. If the container is yours and old, stop it:

bash
docker stop old-app

If you started the new version with docker run and forgot to remove the old one, that is the usual story. With Compose, docker compose up -d replaces its own containers, but a container from a different project name or a manual docker run stays.

Step 3: If it is a host service, decide what to do#

Common cases:

  • A web server on port 80 or 443: nginx or Apache is installed and running, and you are trying to run Caddy or a proxy container on the same port. Only one can win. Either stop the host one (sudo systemctl stop nginx) or move the other to a different port.
  • A leftover of your own app: A previous run is still alive, perhaps in a terminal or a systemd unit. Check systemctl status and stop it the proper way.
  • A system service you need: Do not stop it. Change your service’s port instead.

Use sudo systemctl stop <service> and sudo systemctl disable <service> for a service you do not want at all. Avoid kill -9 as a first move. It skips cleanup, and a supervisor such as systemd or Docker may start the process again at once. If you must kill, send the default signal first: kill <pid>.

Step 4: Change the port instead#

Often the right answer is to avoid the clash. For Docker, change the left side of the mapping, which is the host port:

yaml
services:
  app:
    ports:
      - "127.0.0.1:8081:3000"

Binding to 127.0.0.1 also keeps the port off the public internet, which is what you want behind a reverse proxy. For a host service, change the PORT environment variable or the listen setting in its config. Update anything that points at the old port, such as the proxy config and the firewall.

Step 5: Check for a port that looks free but is not#

Two cases fool people. First, IPv4 and IPv6 are separate listeners: 0.0.0.0:8080 and [::]:8080 can be held by different programs. Check both lines in ss. Second, a listener bound to 127.0.0.1:8080 will clash with a request for 0.0.0.0:8080, because the wildcard overlaps. If the port still seems free, look at ss -ltnp again with the exact address in the message.

If ss shows nothing but the error persists, the socket may be in the middle of closing. TCP connections in TIME_WAIT do not stop a listener from rebinding on most servers, but a program that does not set the reuse option can fail for a short time. Wait a minute and retry.

Check it worked#

Start your service again, then confirm it owns the port:

bash
sudo ss -ltnp | grep 8080
curl -I http://127.0.0.1:8080

The Process column should name your service, and curl should return a response. If you changed ports, also test from outside with curl -I http://203.0.113.10:8081 only if you meant to expose it.

Prevent it#

  • Give each service a fixed, documented host port.
  • Bind internal services to 127.0.0.1 so they do not clash with public ports.
  • Use docker compose down before up when you rename services, so orphans are removed.

Edge cases and gotchas#

  • Ports below 1024: A normal user cannot bind them. That error says permission denied, not address already in use.
  • IPv6 only listeners: A listener on [::]:8080 can also take IPv4 traffic on Linux, so it can block 0.0.0.0:8080.
  • docker-proxy: The userland proxy shows as the owner. The container behind it is the real owner, so find it with docker ps.
  • Compose orphans: Renamed services leave old containers running. docker compose up -d --remove-orphans removes them.
  • Firewall confusion: A blocked port gives timeouts, not this error. This error always means a local listener exists.
  • Crash loops: A service that restarts quickly can fail with this error against its own previous instance still shutting down.

Troubleshooting#

SymptomLikely causeFix
Bind failed on port 80nginx or Apache running on the hostStop it or use another port
port is already allocatedAnother container publishes that host portdocker ps, stop the owner, or change the host port
ss shows nothing, error persistsIPv6 listener or stale socketCheck [::] lines, wait and retry
Process comes back after killA supervisor restarts itStop it through systemd or Docker
Error after renaming a compose serviceOrphan container from the old namedocker compose up -d --remove-orphans
Cannot find the owner without sudoProcess belongs to another userRun ss or lsof with sudo

Quick reference#

bash
sudo ss -ltnp
sudo ss -ltnp | grep :8080
sudo lsof -i :8080
docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker stop <container>
sudo systemctl stop <service>

Next#

Continue with Fix Let’s Encrypt certificate errors.

Try it: Find what holds a port

Track down the owner of port 8080, then see whether it is a container.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command lists listening TCP sockets with the owning process?
2Docker says Bind for 0.0.0.0:80 failed: port is already allocated. What is the first thing to check?
3You want two containers to both serve port 8080 inside, with only one host port 8080. What is the fix?
4Why is stopping the owning service better than kill -9?

Frequently asked questions

What does bind address already in use mean?

A program tried to listen on an address and port that another program already holds. Only one process can listen on a given address and port at a time.

How do I find what is using a port on Linux?

Run sudo ss -ltnp and look for the port, or sudo lsof -i :8080. The output names the process and its PID.

What does Docker's port is already allocated mean?

Docker tried to publish a host port that another container or process already uses. Find the owner with docker ps and ss, then stop it or publish a different host port.

Is it safe to kill the process using the port?

Only if you know what it is. It might be a service you need, such as nginx or a database. Identify it first, and prefer stopping it through systemd or Docker rather than kill -9.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios