# BuildKit cache GC and DinD image maintenance Applies to `.gitea/workflows/docker-publish.yml` from **v1.11.5** onward. Runs on the `gitea-runner-dind` runner against the `gitea-dind` daemon, with the `multiarch` docker-container builder (`buildx_buildkit_multiarch0`, state volume `buildx_buildkit_multiarch0_state`). All commands below run on the Unraid host and target the DinD daemon **explicitly** through `docker exec gitea-dind docker ...`. Never run them as plain `docker ...` on the host: that would target the host daemon that runs the production containers. ## 1. What the workflow does `docker-publish.yml` passes this config to `docker buildx create --buildkitd-config`: ```toml [worker.oci] gc = true reservedSpace = "10GB" # GC never prunes the cache below this maxUsedSpace = "20GB" # GC prunes when the cache grows beyond this minFreeSpace = "50GB" # GC also prunes (down to reservedSpace) when disk free space drops below this ``` - The `reservedSpace`/`maxUsedSpace`/`minFreeSpace` keys need **BuildKit >= v0.17**. Older daemons only understand `gckeepstorage`. `moby/buildkit:buildx-stable-1` is far newer than v0.17. Check the deployed version anyway (section 3). - Sizes are decimal: `20GB` = 20·10⁹ bytes. `buildx inspect` shows it as `18.63GiB`. - `maxUsedSpace` is a **GC threshold, not a quota**. GC runs after builds and periodically, so the cache can briefly exceed 20 GB during a large build. GC removes the least recently used records first, so hot base and dependency layers survive. - `minFreeSpace` uses the free space that `statfs` reports for `/var/lib/buildkit`. On Btrfs that value can be imprecise, so treat it as a safety net. `maxUsedSpace` is the main limit. - With no explicit `[[worker.oci.gcpolicy]]` blocks, BuildKit builds its default policy set from these three values. **The config only applies when buildx creates the BuildKit container.** buildx copies it to `/etc/buildkit/buildkitd.toml` inside the container, and buildkitd loads that path at startup. The file is in the container's filesystem, so it survives `docker restart` and DinD daemon restarts. It is lost only if the container is removed. When the container already exists, the workflow **reuses it unchanged**. It never removes, recreates, restarts or reconfigures a builder, so concurrent jobs cannot race on that. The only race left is two jobs creating a *missing* builder at the same time. That race existed before this change. The losing job now falls back to `docker buildx inspect --bootstrap multiarch`, which never recreates anything. If the running builder has no managed config, every publish job prints a non-fatal warning (`::warning::BuildKit builder 'multiarch' has no managed GC config`). The job log also shows the daemon's GC policy. ## 2. One-time migration of the existing `multiarch` builder (manual) The existing `buildx_buildkit_multiarch0` was created without a config file, so it does **not** pick up the new settings by itself. Run this once, in a maintenance window. Both options keep the state volume and the cache in it. ### Pre-checks (both options) ```bash # 1. No CI job may be running. The only container listed should be buildx_buildkit_multiarch0. docker exec gitea-dind docker ps --format '{{.Names}}\t{{.Image}}\t{{.Status}}' # Hold new jobs: wait until the runner is idle, or stop gitea-runner-dind once it is idle. # Stopping the runner while a job is running aborts that job. # 2. BuildKit version (must be >= v0.17) and current state docker exec gitea-dind docker exec buildx_buildkit_multiarch0 buildctl --version docker exec gitea-dind docker exec buildx_buildkit_multiarch0 cat /etc/buildkit/buildkitd.toml # expected: No such file docker exec gitea-dind docker exec buildx_buildkit_multiarch0 buildctl du | tail -n 3 docker exec gitea-dind docker volume ls --filter name=buildx_buildkit_multiarch0_state ``` ### Option A (recommended): install the config in place, then restart This keeps the same container and volume. It is the same file and path that `buildx create --buildkitd-config` writes. ```bash cat > /tmp/buildkitd.toml <<'EOF' [worker.oci] gc = true reservedSpace = "10GB" maxUsedSpace = "20GB" minFreeSpace = "50GB" EOF docker exec gitea-dind mkdir -p /tmp/bk/buildkit docker cp /tmp/buildkitd.toml gitea-dind:/tmp/bk/buildkit/buildkitd.toml docker exec gitea-dind docker cp /tmp/bk/buildkit buildx_buildkit_multiarch0:/etc/ docker exec gitea-dind docker restart buildx_buildkit_multiarch0 docker exec gitea-dind rm -rf /tmp/bk ``` The config is not active until the restart. Verify it (section 3) before releasing the runner. ### Option B (fallback): remove only the container and let CI recreate it ```bash docker exec gitea-dind docker rm -f buildx_buildkit_multiarch0 # Do NOT use `docker volume rm`, `docker buildx rm` without --keep-state, or `docker system prune --volumes`. ``` The next publish job runs `buildx create --buildkitd-config`. That creates a new container, which mounts the **existing** named volume `buildx_buildkit_multiarch0_state`, so the cache is kept. Start a single publish job first, so that parallel jobs do not race to create the container. Use this option only if Option A cannot be verified. ## 3. Verification Of the commands below, only the running daemon's GC policy proves that GC is configured. `buildctl du` and `df` show usage, not configuration. ```bash # Authoritative: the GC policy the running buildkitd uses docker exec gitea-dind docker exec buildx_buildkit_multiarch0 buildctl debug workers -v # expected: GC Policy rules showing Reserved Space 10GB / Max Used Space 20GB / Min Free Space 50GB # (some versions print these as 9.313GiB / 18.63GiB / 46.57GiB). Without the config you see # percentage-derived defaults instead. # The same data appears in every publish job log ("GC Policy" section of `docker buildx inspect multiarch`). # The file that buildkitd loads at startup (shows it exists, not that it is active) docker exec gitea-dind docker exec buildx_buildkit_multiarch0 cat /etc/buildkit/buildkitd.toml # Cache usage: total should settle around 20 GB after GC runs docker exec gitea-dind docker exec buildx_buildkit_multiarch0 buildctl du | tail -n 3 # Image store and disk docker exec gitea-dind docker system df df -h /mnt/cache ``` ## 4. DinD image retention (proposal, not automated) BuildKit GC manages **build cache** in the builder's state volume only. It never removes images from the `gitea-dind` image store. Those images are a separate problem. **Where the images come from:** the scan step runs `buildx build --load -t /:`. That loads one *tagged* image into the DinD store per publish run, and nothing removes it. Because these images are tagged, a dangling-only prune (`docker image prune --filter until=168h`) does **not** reclaim them. It only removes images whose SHA tag was later reused. The 436 accumulated images are mostly these per-commit scan images. Proposed policy: one central job that runs from the Unraid host (for example Unraid "User Scripts", weekly). It is not part of every pipeline, because pipelines in different repositories cannot lock against each other. ```bash #!/bin/bash # Targets ONLY the gitea-dind daemon. Never deletes volumes or build cache. set -euo pipefail D="docker exec gitea-dind docker" # Guard: skip if any CI job container is running (anything besides the BuildKit builder). busy="$($D ps --format '{{.Names}}' | grep -v '^buildx_buildkit_' || true)" if [ -n "$busy" ]; then echo "CI busy, skipping: $busy"; exit 0; fi $D image ls --format '{{.Repository}}:{{.Tag}}\t{{.CreatedSince}}\t{{.Size}}' # audit log $D image prune -a -f --filter "until=168h" # unused images (tagged + dangling) created > 7 days ago $D system df ``` Safety properties and caveats: - `image prune -a` skips images that any container uses, running or stopped. That includes the BuildKit image. It never touches volumes or BuildKit cache. - The idle guard cannot close the race completely. A job could start between the check and the prune. The impact is limited: `until=168h` filters on image *creation* time, and a fresh scan image is normally minutes old. A fully cached rebuild can, however, carry an older creation time, which in the worst case breaks the Trivy step of a job that starts exactly then. Re-running the job fixes it. For a zero-risk window, stop `gitea-runner-dind` while it is idle around the prune. - The runner job image (`gitea_runner_python314`), `tonistiigi/binfmt` and other base images are removed when idle and re-pulled on the next job. That costs bandwidth, not correctness. - Run it once in dry-run mode (only the `image ls` line) and review the list before you enable the prune. - Optional follow-up (not part of this release): remove the scan image at the end of each publish job. This needs a per-run unique local tag, because concurrent branch and tag pushes of the same commit share `:`. Do not use `docker system prune --volumes` or `docker volume prune`. Do not use `docker buildx prune` as routine maintenance; GC now covers the cache. **Separate finding (not changed here):** `gitea-dind` exposes an unencrypted Docker API on port 2375. Anyone who can reach that port has root-equivalent control of the DinD daemon. This change does not widen that exposure. Restricting it (bind to the runner network only, or use TLS on 2376) should be tracked as its own infrastructure change. ## 5. Rollout and rollback - Release as **v1.11.5** (patch release: no input, output or secret changes, so it is backward compatible). Existing tags stay unchanged. - Consumers move from `@v1.11.x` to `@v1.11.5` at their own pace. `werkplan-web` is still on `v1.11.1`; going to v1.11.5 also brings in the v1.11.2–v1.11.4 fixes (Trivy DB source, buildx fallback pin, private-index handling, notifications). - The workflow change alone does **not** fix the existing builder. The one-time migration in section 2 is required. - Rollback of the workflow: pin consumers back to `@v1.11.4`. v1.11.4 reuses the existing container, so a builder that is already migrated keeps its GC config. That is harmless. - Rollback of the builder config, in a maintenance window: `docker exec gitea-dind docker exec buildx_buildkit_multiarch0 rm /etc/buildkit/buildkitd.toml` followed by `docker exec gitea-dind docker restart buildx_buildkit_multiarch0`. buildkitd then uses its built-in defaults again. The cache volume is kept.