Allow multiple/alternative filter flags in clear-docker-cache script (closes #39234)
What does this MR do?
Adds per-type filter flags to the clear-docker-cache script, giving users granular control over which containers, images, and volumes are pruned.
Previously, the script used a single FILTER_FLAG with docker system prune -af. Because docker system prune --filter only applies to containers, images and volumes were always pruned unconditionally — users could not keep specific images or volumes.
Now the script uses targeted prune commands (container prune, image prune, volume prune) and introduces three independent environment variables:
CONTAINER_FILTER_FLAGS— controls which containers are pruned (default:label=com.gitlab.gitlab-runner.managed=true)IMAGE_FILTER_FLAGS— controls which images are pruned (default: empty → all unused images)VOLUME_FILTER_FLAGS— controls which volumes are pruned (default: empty → all unused volumes)
Each variable accepts comma-separated values which are converted to multiple --filter flags. For example:
IMAGE_FILTER_FLAGS="label=keep,label!=provider=Acme" clear-docker-cacheThis translates to docker image prune -af --filter=label=keep --filter=label!=provider=Acme.
FILTER_FLAG is preserved for backward compatibility. When set, it is used as the default for CONTAINER_FILTER_FLAGS if that variable is not explicitly set.
Why was this MR needed?
Users running multi-architecture CI jobs or custom Docker setups on GitLab Runner hosts needed the ability to preserve specific Docker images (e.g., large base images) from the prune cycle triggered by clear-docker-cache. The single FILTER_FLAG could not achieve this because docker system prune ignores filter flags for images and volumes.
What's the best way to test this MR?
- Run the script with custom per-type filters:
CONTAINER_FILTER_FLAGS="label=foo=bar" IMAGE_FILTER_FLAGS="label=keep=true" clear-docker-cache prune - Verify
docker container prune -f --filter=label=foo=baranddocker image prune -af --filter=label=keep=trueare executed - Run with an explicit
FILTER_FLAGand verify it still applies to containers:FILTER_FLAG="label=my-custom=true" clear-docker-cache - Run
bash -nto confirm script syntax:bash -n packaging/root/usr/share/gitlab-runner/clear-docker-cache
What are the relevant issue numbers?
Closes #39234 (closed)