GitLab CI "Failed to remove network for build" in CI
After a job the Docker executor tries to delete the temporary network it created and fails because a container is still attached or the network is in use. The job may still pass, but cleanup errors accumulate.
What this error means
The job log ends with "ERROR: Failed to remove network for build" or the runner logs "error while removing network: network ... has active endpoints".
ERROR: Failed to remove network for build error=networks.id=abcd... cannot be removed: network has active endpointsCommon causes
A leftover container holds the network
A service or build container did not exit cleanly, so the network still has active endpoints and cannot be removed.
Docker state pressure on the runner host
Accumulated dangling networks and containers on a busy host make cleanup flaky.
How to fix it
Prune leftover Docker state on the host
- On the runner host, remove dangling containers and networks.
- Schedule periodic pruning so state does not accumulate.
- Re-run the affected job.
docker container prune -f
docker network prune -fReduce build container leaks
Ensure services stop cleanly and avoid detaching long-lived containers inside jobs that outlive the build network.
How to prevent it
- Prune Docker networks/containers on runner hosts on a schedule.
- Make sure job services exit cleanly at job end.
- Monitor runner hosts for accumulating dangling networks.