ForestMuffin

Fix Docker ‘address already in use’: lsof, stop, or remap -p

Dev Notes4 min read

If docker-compose up just killed your Postgres or Elasticsearch container with “bind: address already in use” or “driver failed programming external connectivity,” something else already holds the host port it wants. A handful of commands gets it running, and nothing else gets deleted.

The short answer

As of November 2026, “address already in use” means another process holds the host port your container wants. Read that port from 0.0.0.0:<port> in the error, then stop the holder or remap to a free host port.

AI-generated image: A developer seen from behind works at a desk late at night, lit by a blurred monitor and a desk lamp.
When a container refuses to start, the fix begins with one port number in the error. (AI-generated image, not a real still)
sudo lsof -i :<port_number>

The output shows the process name, user and command. If it’s a container, docker stop it. If it’s a host process, use a plain kill <PID>. Or leave it alone and publish a free port instead, such as -p 8082:5432.

One limit up front: the commands below come from community posts, and none of them state a Docker Engine or Compose version. The rest of this post walks through the fix and the spots where people get stuck.

Step 1: read the port from the error

Here’s what Docker printed when the elk_elasticsearch_1 service failed to start:

Error starting userland proxy: listen tcp4 0.0.0.0:9300: bind: address already in use

The number after 0.0.0.0: is the host port in conflict, here 9300. Yours will differ. In listen tcp 0.0.0.0:8080: bind: address already in use, it’s 8080. Whatever sits at the end of listen tcp 0.0.0.0:<port> is the number you search for next.

You see this when you run docker run or docker-compose up with a published port and the container never comes up. Docker tries to bind the host side of the mapping. It can’t share a port another process already uses, so it refuses and the container doesn’t start.

Step 2: find what holds the port

This step decides everything else, so it gets the most words. Run lsof against the port:

AI-generated image: Close-up of ethernet cables plugged into a switch, with one port glowing amber among green lights.
Before you change anything, find out exactly which process is sitting on the port. (AI-generated image, not a real still)
sudo lsof -i -P -n | grep <port_number>

Keep the sudo. Without it, lsof may not list system processes, so the holder can be missing from the output. That may be why lsof shows nothing when you know something is on the port. Use sudo lsof -i :8080, swapping in the port from your own error message.

Getting “command not found”? On Linux, the lsof package isn’t installed by default. The source installs it with yum install -y lsof, which fits RHEL and CentOS style systems, so use your own package manager elsewhere. netstat gives you the PID and process name as well:

sudo netstat -antp | grep <port_number>

Whichever you run, sudo lsof -i -P -n | grep <port> or sudo netstat -antp | grep <port>, you’re reading the same two things: process name and PID. The sources name three possible holders:

  • Another Docker container using the same port.
  • A non-Docker process running directly on the host.
  • A previous container that was stopped, but the port wasn’t properly released.

The third is the easy one to miss. The sources don’t explain why a port fails to release, so treat it as a possibility to check, not a diagnosis. One author says it mostly happened when they killed containers instead of bringing them down properly.

They also admit they don’t know the exact cause. That’s a hunch, not a rule, so let the lookup output tell you who holds the port.

You might also hit “port is already allocated.” One community source says that message appears when another Docker container already uses the port, while “address already in use” means a host process holds it. That rests on a single post, so confirm with docker ps before you act on it.

If you’re unsure what a process is, look it up before you end it.

Step 3: stop the holder, or move your container

Pick one of these, starting with the least destructive.

Flowchart: after sudo lsof identifies the port holder, there are three paths. Stop a container with docker stop, kill a host process with kill, or remap to a free host port with -p 8082:5432. All three paths then remove the failed container and rerun the original command.
Pick the branch that matches the holder that lsof reports.
Diagram of the mapping -p 8082:5432, in which host port 8082 connects to port 5432 inside the container.
Remapping changes only the host side. The container port stays 5432.
AI-generated image: A harbor crane lifts a plain shipping container toward an empty spot on the dock at sunset.
If the occupied spot isn’t yours to clear, move your container to a free port instead. (AI-generated image, not a real still)

If another container holds the port, stop only that one:

docker stop <container_name_or_id>

Don’t run docker stop $(docker ps -a -q) to clear one port. It hits every container on the machine and takes down unrelated work.

If a host process holds it, end it by the PID from the lsof output. Try a plain kill first:

kill <PID>

In the source example, a plain kill 78812 freed port 8080 and the container started without a conflict. Some guides jump straight to kill -9 <PID>. Treat that as a last resort, and keep -9 for a process that ignores a plain kill.

If you don’t want to touch the holder, map your container to a host port that isn’t in use:

docker run -p 8082:5432 <image>

Here 8082 is the free host port. The container port, 5432 for Postgres, stays the same. To pick a number, check the open ports with ss or netstat and choose one that’s free. The sources say you can make the same change in your Docker Compose file, so edit the port mapping there if that’s how you start the container.

So which one? If the holder is a leftover container of yours, stop it. If it’s something you need running, remap instead, so you don’t have to touch the holder at all.

Step 4: clear the failed container and start again

The container failed to start, so the source removes it before trying again. Use the container’s name:

docker rm -f postgresql-baeldung

postgresql-baeldung is the container name from the source’s example. Replace it with yours, so the command becomes docker rm -f <name>. Remove only that container, not everything from docker ps -a. The bulk version, docker rm -fv $(docker ps -aq), hits every container, and the -v flag also deletes volumes, which means lost data.

Then run your original docker run or docker-compose up command, with the new -p mapping if you remapped the port. If the same error comes back, read the port from the new message and repeat step 2.

Sources

This article was created using generative AI based on publicly available sources.