Compose "Bind for 0.0.0.0:PORT failed: port is already allocated" in CI
Compose tried to publish a service port to the host but the host port is already bound, by a leftover container from a prior job, another compose stack, or a service the runner already runs. The bind fails and the service does not start.
What this error means
A docker compose up fails with "Error response from daemon: driver failed programming external connectivity ... Bind for 0.0.0.0:<port> failed: port is already allocated".
Error response from daemon: driver failed programming external connectivity on
endpoint app_db_1: Bind for 0.0.0.0:5432 failed: port is already allocatedCommon causes
A leftover container still holds the port
A previous job did not tear down its stack, so an old container is still bound to the host port.
Another process or stack uses the same host port
A second compose project or a runner service maps the same port, so the second bind is refused.
How to fix it
Tear down old stacks before starting
- Run
docker compose down(with--remove-orphans) beforeup. - Use a unique project name per job so stacks do not collide.
- Re-run; the port is now free to bind.
docker compose down --remove-orphans
docker compose up -dLet the host port be assigned dynamically
In CI you rarely need a fixed host port; publish to an ephemeral host port to avoid collisions.
services:
db:
ports:
- "5432" # host port chosen automaticallyHow to prevent it
- Always
docker compose downat job start or end. - Use unique project names so stacks do not clash.
- Avoid pinning host ports in CI; let Docker assign them.