Virtualization

Docker: container fails to start with 'bind: address already in use' when publishing port

Environment

Environment

Ubuntu 22.04 host running Docker Engine 24.x; container image is a Node.js web server publishing port 3000; deployment via docker run -p 3000:3000.

Symptoms

Symptoms

docker run fails immediately with an error mentioning bind/address in use; container does not start and no application logs are produced inside the container.

Observations

Observations

The host already runs other services (systemd unit and a test nginx). netstat shows entries for tcp 0 0 0.0.0.0:3000 LISTEN. docker ps shows no container binding 3000 because the run aborted.

Already attempted

Already attempted

Stopped a local test service on port 3000 and re-ran docker run; the container then started successfully. No changes were made to firewall or iptables rules.

Diagnostic steps

Diagnostic steps

  1. Run sudo ss -lntp | grep :3000 (or sudo lsof -iTCP:3000 -sTCP:LISTEN) to identify the process and PID listening on the host port.
  • What the result means: If output shows a PID and process name (systemd, nginx, node, etc.), that process is the current owner and must be stopped or reconfigured. If no process is shown but kernel shows TIME_WAIT only, a previous connection may linger (temporary). If docker-proxy appears, an existing container or stale mapping is holding the port.
  1. List Docker containers and their published ports: docker ps --format '{{.ID}} {{.Names}} {{.Ports}}'.
  • What the result means: If an existing container publishes the same host port, stop or remove that container to free the port. If no container shows the port, the listener is a host process.
  1. Check systemd for services configured to use the port: sudo systemctl list-sockets | grep 3000 and inspect unit files in /etc/systemd/system and /lib/systemd.
  • What the result means: If a systemd socket unit owns the port, disable or reconfigure the unit instead of killing processes directly; disabling is reversible with systemctl enable/start unit restore.
  1. Attempt binding container to a different host port temporarily: docker run -p 127.0.0.1:3001:3000 image and verify the service is reachable on localhost:3001.
  • What the result means: If the container runs and is reachable, the problem is a host-level port conflict; using an alternate host port is a safe workaround. If binding to 127.0.0.1 succeeds but 0.0.0.0 fails, an external service is occupying the 0.0.0.0 address.
Confirmed fixes

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

Question

What steps will reliably identify which process owns the port, whether Docker or host service is responsible, and how to map or free the port safely in production without disrupting unrelated services?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Host process (systemd service or user process) is already listening on the requested host port 85%
A previously started container bound the port but terminated abnormally leaving the host socket in TIME_WAIT or a listening proxy (rare) 40%
Docker daemon-level socket or conflict with host network namespace mapping (host networking mode) 30%

Summary

Port binding collisions can occur when a host process already listens on the requested host port or when Docker's socket proxy from an existing container/supervisor holds the port. Diagnosis requires identifying the current listener, checking Docker port-mapping conflicts and systemd services, and validating whether binding to a different host interface (127.0.0.1 vs 0.0.0.0) is acceptable.

Suggested Fixes

Diagnostic step 1
Run sudo ss -lntp | grep :3000 (or sudo lsof -iTCP:3000 -sTCP:LISTEN) to identify the process and PID listening on the host port. Result meaning: If output shows a PID and process name (systemd, nginx, node, etc.), that process is the current owner and must be stopped or reconfigured. If no process is shown but kernel shows TIME_WAIT only, a previous connection may linger (temporary). If docker-proxy appears, an existing container or stale mapping is holding the port.
Diagnostic step 2
List Docker containers and their published ports: docker ps --format '{{.ID}} {{.Names}} {{.Ports}}'. Result meaning: If an existing container publishes the same host port, stop or remove that container to free the port. If no container shows the port, the listener is a host process.
Diagnostic step 3
Check systemd for services configured to use the port: sudo systemctl list-sockets | grep 3000 and inspect unit files in /etc/systemd/system and /lib/systemd. Result meaning: If a systemd socket unit owns the port, disable or reconfigure the unit instead of killing processes directly; disabling is reversible with systemctl enable/start unit restore.

Continue troubleshooting

Matched from the title, description and tags