More apps from usSnipora

Guide

Fix Docker “port is already allocated” on Mac

docker run or docker compose up stops with a port error. The wording tells you who holds the port: another container, or a program on your Mac. Here’s how to tell, and how to stop it coming back.

By Anmol Sethi · Updated

In short

“port is already allocated” means another container publishes that port: find it with docker ps --filter publish=5432 and docker stop it. “Ports are not available … address already in use” means a Mac process holds it, often a Homebrew database: find it with lsof -nP -iTCP:5432 -sTCP:LISTEN. Or publish on another host port, such as -p 5433:5432.

Which error do you have?

ErrorWho holds the port
Bind for 0.0.0.0:5432 failed: port is already allocatedAnother container. Go to step 1.
Ports are not available: exposing port TCP 0.0.0.0:5432 … bind: address already in useA program on your Mac. Go to step 2.

The examples use 5432 (Postgres); swap in your port: 3306 for MySQL, 6379 for Redis, 3000 or 8080 for an app.

Rather click than type? Portlivo shows every port with its project and frees it in one click. Try it free for 7 days →

1. Another container has it

docker ps --filter publish=5432
CONTAINER ID   IMAGE         …   PORTS                    NAMES
3f9c1a2b7d4e   postgres:16   …   0.0.0.0:5432->5432/tcp   shop-db-1

Usually it’s the same service from another project’s Compose file, or from an older checkout. Stop just that container, or the whole project it belongs to:

docker stop shop-db-1

# or the whole Compose project, from anywhere:
docker compose ls
docker compose -p shop down

docker compose down stops and removes the containers but keeps named volumes, so the database’s data survives. Add -v only if you want the data gone.

2. A program on your Mac has it

lsof -nP -iTCP:5432 -sTCP:LISTEN
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
postgres 611 you    7u  IPv6  0x4e19…      0t0  TCP [::1]:5432 (LISTEN)
postgres 611 you    8u  IPv4  0x9b02…      0t0  TCP 127.0.0.1:5432 (LISTEN)

The classic case is a database installed with Homebrew that starts at login. Killing it isn’t enough, because launchd starts it again. Stop it through Homebrew, which also keeps it from starting at the next login:

brew services list
brew services stop postgresql@16

Postgres.app, DBngin and Laravel Herd run databases too; quit them from their own menu. A dev server on the port is a plain leftover: see How to kill the process on a port. On 5000 or 7000, it’s AirPlay Receiver.

3. Or publish on another host port

If you want both running, say Homebrew Postgres for one project and a container for another, change only the host side, the number before the colon:

docker run -p 5433:5432 postgres:16
# compose.yaml
services:
  db:
    image: postgres:16
    ports:
      - "${DB_PORT:-5432}:5432"

Then DB_PORT=5433 docker compose up, and connect to localhost:5433 from your Mac. Other containers in the same Compose project still reach it as db:5432.

It’s taken again after a restart

  • A restart policy. Containers started with restart: always or unless-stopped come back whenever Docker starts. Check with docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' shop-db-1 and turn it off with docker update --restart=no shop-db-1.
  • A Homebrew service. brew services stop unregisters it; kill doesn’t. See Homebrew services.
  • Nothing holds it, but Docker still says allocated. Docker Desktop occasionally keeps a port after a container crashed. docker ps -a shows nothing using it; quit and reopen Docker Desktop (or OrbStack) to release it.

The one-click way: Portlivo

Portlivo lists Docker and OrbStack containers by name next to your dev servers and Homebrew services, with the host port each one publishes. Stopping a container runs docker stop and stopping a service runs brew services stop, so neither comes straight back.

Questions people ask

What does “port is already allocated” mean in Docker?
Another container already publishes that host port. docker ps --filter publish=<port> finds it; docker stop frees the port.
What does “Ports are not available … address already in use” mean?
A program on your Mac, not a container, listens on that port. lsof -nP -iTCP:<port> -sTCP:LISTEN names it.
How do I run Docker Postgres when Postgres is already installed on my Mac?
Publish the container on another host port, -p 5433:5432, and connect to localhost:5433. Or stop the local one with brew services stop.
Does docker compose down delete my database?
No. It removes containers and networks but keeps named volumes. Only docker compose down -v deletes them.

Try Portlivo free for 7 days. Every feature, no account.

Download for Mac

Still have a question?

Write to us — a real developer reads every email and replies personally.

support@portlivo.com