πŸ”§ Error Fixes

Docker: Port Already in Use β€” 3 Quick Fixes


Error response from daemon: driver failed programming external connectivity:
Bind for 0.0.0.0:3000 failed: port is already allocated

Or:

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

Something else is already using the port you’re trying to map. Here’s how to fix it.

Fix 1: Find and Kill the Process Using the Port

macOS / Linux:

# Find what's using port 3000
lsof -i :3000

# Output:
# COMMAND  PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
# node    1234   user   23u  IPv4  ...    TCP *:3000 (LISTEN)

# Kill it
kill 1234

# Force kill if it won't stop
kill -9 1234

Windows:

netstat -ano | findstr :3000
taskkill /PID 1234 /F

Fix 2: Use a Different Port

Just map to a different host port:

# Instead of:
docker run -p 3000:3000 myapp

# Use:
docker run -p 3001:3000 myapp

Your app still runs on port 3000 inside the container, but you access it on localhost:3001.

Docker Compose:

services:
  web:
    ports:
      - "3001:3000"  # changed host port

Fix 3: Stop the Other Docker Container

Often it’s another Docker container using the port:

# See all running containers
docker ps

# Find the one using port 3000 and stop it
docker stop <container-name>

# Or stop ALL containers
docker stop $(docker ps -q)

Docker Compose β€” stop old services first:

docker compose down
docker compose up -d

Prevent This in the Future

Use dynamic ports in development:

docker run -p 0:3000 myapp
# Docker assigns a random available port
# Check which port: docker ps

Use environment variables for ports:

# docker-compose.yml
services:
  web:
    ports:
      - "${PORT:-3000}:3000"

Then: PORT=3001 docker compose up -d