CI cleanup is usually treated as housekeeping. In a busy Docker pipeline, that is the wrong mental model. A container, temporary database, test inbox, or cloud runner left behind can change the next build, inflate costs, and make an incident harder to explain.
I started treating cleanup as part of the job's reliability contract. The job must say what it owns, how long the ownership lasts, and what evidence proves that the resource was released. This made our pipelines less mysterious, especially when a build was cancelled halfway through a deploy check.
The cleanup problem is a reliability problem
Consider a CI job that builds an image, starts a Compose stack, creates test data, and runs browser checks. The happy path is easy. The hard paths are a failed test, a runner eviction, a manual cancellation, and a timeout while the job is waiting for an external service.
On those paths, cleanup code may never run, or it may run without enough context to find the resources.…