Quick Summary: On macOS Monterey and newer, attempting to start a local development server, Flask API, or Docker container on port
5000(or7000) frequently fails withAddress already in useorPort 5000 is in use by another program. This conflict is caused by Apple’s built-in AirPlay Receiver service (ControlCenter), which binds to TCP ports 5000 and 7000 by default and automatically respawns if killed manually.
Understanding the Root Causes
Many web frameworks and DevOps tools (such as Python Flask, MLflow, Docker Registry, and .NET Kestrel) default to port 5000. However, since macOS 12 Monterey, macOS enables AirPlay Receiver out of the box so your Mac can act as an AirPlay display or speaker. Key behaviors causing the conflict include:
- ControlCenter Binding Ports 5000 and 7000: The macOS
ControlCenterprocess opens TCP listeners on both IPv4 (*:5000) and IPv6 (*:5000) sockets. - Automatic Launchd Respawn: Running
kill -9 <PID>onControlCenteronly frees the port for a fraction of a second becauselaunchdimmediately restarts the system UI daemon. - HTTP 403 Forbidden Responses: If your server fails silently or you query
localhost:5000expecting your app, AirPlay Receiver intercepts the HTTP request and returns an empty403 Forbiddenresponse withServer: AirTunes/...headers.
Step 1: Quick Triage – Confirm ControlCenter is Holding Port 5000
Before changing system settings, verify which process owns port 5000 using lsof in Terminal:
# Inspect processes listening on TCP port 5000 (and 7000)
lsof -i :5000
# Example output confirming AirPlay Receiver conflict:
# COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
# ControlCe 12345 user 7u IPv4 0x1a2b3c4d5e6f7a8b 0t0 TCP *:commplex-main (LISTEN)
# ControlCe 12345 user 8u IPv6 0x9b8a7c6d5e4f3a2b 0t0 TCP *:commplex-main (LISTEN)
# Confirm via curl header inspection
curl -I http://localhost:5000
# Output showing AirTunes:
# HTTP/1.1 403 Forbidden
# Server: AirTunes/740.12.1
Step 2: Disable AirPlay Receiver in macOS System Settings
The permanent, clean way to free ports 5000 and 7000 on macOS without affecting other Control Center features is disabling AirPlay Receiver:
- Open System Settings (Apple menu → System Settings).
- Navigate to General → AirDrop & Handoff (on macOS Monterey, go to System Preferences → Sharing).
- Toggle AirPlay Receiver to Off.
Alternatively, you can disable it via Terminal if you are scripting workstation setup:
# Disable AirPlay Receiver via defaults (may require reboot or ControlCenter restart)
defaults -currentHost write com.apple.controlcenter AirplayRecieverEnabled -bool false
# Restart ControlCenter to apply immediately
killall ControlCenter
Step 3: Remap Your Application or Docker Port (Alternative)
If you actively use your Mac as an AirPlay Receiver and want to keep it enabled, configure your local service or Docker container to listen on an unreserved port such as 5001 or 8000:
# Python Flask CLI:
flask run --port=5001
# Docker run port mapping (host 5001 -> container 5000):
docker run -d -p 5001:5000 --name my-api my-image:latest
# In docker-compose.yml:
# ports:
# - "5001:5000"
Verification & Testing Steps
Confirm that port 5000 is completely released and test starting your local server:
# 1. Verify no process is listening on port 5000
lsof -i :5000
# Expected output: (empty)
# 2. Test binding a quick Python HTTP server on port 5000
python3 -m http.server 5000 &
SERVER_PID=$!
# 3. Verify local connectivity and stop the test server
curl -I http://localhost:5000
kill $SERVER_PID
Troubleshooting Summary Comparison
| Symptom / Scenario | Primary Root Cause | Recommended Solution |
|---|---|---|
lsof -i :5000 shows ControlCe |
macOS AirPlay Receiver bound to port 5000 | Disable AirPlay Receiver in System Settings |
curl localhost:5000 returns 403 AirTunes |
AirPlay HTTP server intercepting local traffic | Turn off AirPlay Receiver or switch app to port 5001 |
Process restarts right after kill -9 |
launchd respawns ControlCenter automatically |
Disable via System Settings instead of killing PID |
| Need AirPlay Receiver active | Ports 5000 & 7000 reserved while enabled | Use -p 5001:5000 in Docker or --port=5001 in CLI |
Leave a Reply