docker / Guides

Docker port already allocated: find and resolve the conflict

A published port uses an address and port on the Docker host. Another container or host application may already be listening there. Changing the host port leaves the container’s internal port unchanged.

Examples use Bash; consult the references for details of your environment.

Find the listener before stopping anything

List running containers and their published ports. Look for the host side of a mapping, such as 8080->80/tcp. On a Linux host, ss -ltnp also lists TCP listeners; some process details require suitable privileges.

docker ps --format 'table {{.Names}}\t{{.Ports}}'

An empty container list does not prove the port is free: a host application may own it. Check the address and protocol too. Identify the service and its purpose before stopping it.

Choose an available host port

For this disposable web example, first check that host port 8081 is available and that the name devsheet-web is unused. The example binds to the host’s loopback address and starts nginx in the foreground.

docker run --rm --name devsheet-web -p 127.0.0.1:8081:80 nginx

The mapping is HOST_IP:HOST_PORT:CONTAINER_PORT. Here only the host port changes to 8081; nginx still listens on container port 80. Open another terminal to test it. Ctrl+C stops this demo, and --rm removes its container.

Update the mapping in Compose

In your Compose file, change the intended service’s ports entry to 127.0.0.1:8081:80, after checking that port. Recreate the reviewed service to apply the mapping; replace web below with its actual service name.

docker compose ps
docker compose up -d web

A simple restart does not apply a changed container port mapping. Check the Compose project before recreating a service, and preserve its mounts and application data. Avoid stopping unrelated containers to free a port.

Verify the published service

Check the running mapping and send an HTTP request to the chosen host port. The nginx demo should answer; other applications may need time to start.

docker ps --format 'table {{.Names}}\t{{.Ports}}'
curl --head http://127.0.0.1:8081/

If the bind conflict persists, recheck the host address, port and protocol, including IPv4 and IPv6 listeners. An HTTP application error after connection is different from a port binding failure. Review the intended service logs and configuration.

References

All guides