Replace the chained inspect||create||inspect with explicit steps: register builder metadata without --bootstrap (create errors fail the job), bootstrap with a single logged retry for concurrent creation, and verify the GC policy reported by the running BuildKit daemon. A builder created by this job without the expected policy fails the job; an existing unmigrated builder gets a warning pointing to the manual migration. The temporary buildkitd config is removed on exit. Existing containers and state volumes are never recreated or removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 KiB
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:
[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/minFreeSpacekeys need BuildKit >= v0.17. Older daemons only understandgckeepstorage.moby/buildkit:buildx-stable-1is far newer than v0.17. Check the deployed version anyway (section 3). - BuildKit parses the sizes with go-units. Depending on the version,
docker buildx inspectshows20GBas20GiB(binary) or18.63GiB(decimal). The workflow check accepts both. maxUsedSpaceis 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.minFreeSpaceuses the free space thatstatfsreports for/var/lib/buildkit. On Btrfs that value can be imprecise, so treat it as a safety net.maxUsedSpaceis 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 setup step runs these steps in order, and none of them hides an error:
- If this job has no buildx metadata for
multiarch, rundocker buildx create --buildkitd-configwithout--bootstrap. If that fails, the job fails. - Run
docker buildx inspect --bootstrap multiarch. This starts the existing container, or creates it if it is missing. If it fails, the step retries once after 10 seconds. That retry covers two jobs creating a missing builder at the same moment. A second failure fails the job with buildx's own error. - Check the GC policy that the running daemon reports (
docker buildx inspect multiarch; the job log shows the "GC Policy" section):- all three limits match: logs
BuildKit GC config is active; - this job created the container from the config, but the limits do not match: job
fails (
::error::); - the builder already existed without the config: non-fatal warning that the managed GC limits are not active and the one-time migration below is required. The build continues unchanged.
- all three limits match: logs
- Delete the temporary config file when the step exits.
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)
# 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.
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 you release the runner.
If the daemon does not report the expected policy, or the container does not come back up,
delete the file again and restart the container (see Rollback in section 5). The cache volume
is not affected either way. Once the runner is back in service, the next v1.11.5 publish job
should log BuildKit GC config is active instead of the migration warning.
Option B (fallback): remove only the container and let CI recreate it
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. That first job must use @v1.11.5 or later. A consumer still on v1.11.4 or older
recreates the container without the config. 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.
# 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 with Reserved Space / Max Used Space / Min Free Space of
# 10GiB / 20GiB / 50GiB (or 9.313GiB / 18.63GiB / 46.57GiB, depending on the version).
# 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 <registry>/<image>:<sha>.
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.
#!/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 -askips 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=168hfilters 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, stopgitea-runner-dindwhile it is idle around the prune. - The runner job image (
gitea_runner_python314),tonistiigi/binfmtand 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 lsline) 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
<image>:<sha>.
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.xto@v1.11.5at their own pace.werkplan-webis still onv1.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.tomlfollowed bydocker exec gitea-dind docker restart buildx_buildkit_multiarch0. buildkitd then uses its built-in defaults again. The cache volume is kept.