Sub topic 10
Scopes, names, and branch fallbacks
This chapter assumes you can already run docker buildx build with --cache-from and --cache-to, and that you know the registry, gha, and s3 backends at a basic level. Here you will learn how to keep the caches of different builds apart, and how to let a new branch reuse the cache from main.
Why a single cache location breaks
Every backend stores cache at one location, and a build that exports to that location replaces what was there. If several builds share it, they overwrite each other. This happens with:
- a repository that builds two images with different Dockerfiles
- a matrix job that builds for several platforms
- feature branches that all write to the same place
The result is a cache that exists but rarely matches the build that reads it.
Where each backend puts the name
Each backend has its own option for naming a cache location.
| Backend | Option | Default | What it identifies |
|---|---|---|---|
| registry | `ref=` | none (required) | A full image reference, usually a dedicated tag |
| gha | `scope=` | `buildkit` | A named cache entry in the GitHub cache service |
| s3 | `name=` | `buildkit` | A manifest name inside the bucket |
For the S3 backend, prefix= also puts the cache under a folder in the bucket. That is useful when several projects share one bucket.
Separate by image, share by branch
Use one scope per image or platform. Add the branch only when you want isolation between branches. Branch isolation has a cost, because a new branch starts with an empty scope. The fix is a fallback: write to your own scope, but read from your own scope and then from main.
--cache-from can be given more than once. BuildKit imports each source and reuses any layer whose cache key matches. Only --cache-to decides where results are written, so keeping main out of --cache-to on feature branches protects it.
A GitHub Actions example
This workflow builds a branch name, then uses it in the scope. The ref values go through env instead of being pasted into the script, so a hostile branch name cannot inject shell commands. Slashes are replaced because names like feature/login are not valid in registry tags, and they make cache names awkward elsewhere.
name: build
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Compute cache branch name
env:
HEAD_REF: ${{ github.head_ref }}
REF_NAME: ${{ github.ref_name }}
run: |
branch="${HEAD_REF:-$REF_NAME}"
echo "BRANCH=$(echo "$branch" | tr '/' '-')" >> "$GITHUB_ENV"
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ci
cache-from: |
type=gha,scope=myapp-${{ env.BRANCH }}
type=gha,scope=myapp-main
cache-to: type=gha,scope=myapp-${{ env.BRANCH }},mode=maxOn pull requests, github.head_ref holds the source branch. On pushes it is empty, so the script falls back to github.ref_name. GitHub also limits which caches a run can read. A pull request can read caches from its own branch and from the base branch, but not from sibling branches.
The same idea with a registry
For the registry backend, the scope is the tag. Give each branch its own cache tag and fall back to the main tag.
BRANCH=feature-login
docker buildx build \
--cache-from type=registry,ref=registry.example.com/myapp:cache-$BRANCH \
--cache-from type=registry,ref=registry.example.com/myapp:cache-main \
--cache-to type=registry,ref=registry.example.com/myapp:cache-$BRANCH,mode=max \
-t registry.example.com/myapp:$BRANCH \
--push .If the first cache-from tag does not exist yet, the import of that source fails and BuildKit continues with the others. The s3 backend works the same way, with name=myapp-feature-login and name=myapp-main.
Exercise
A repository builds a web image and an api image from two Dockerfiles in one workflow, both with type=gha,scope=myapp and mode=max. Builds are often cache misses even though nothing changed. What is the likely cause, and what is the fix?
Show answer
Both images export to the same scope, so each build overwrites the cache the other one just wrote. Give each image its own scope, for example scope=myapp-web and scope=myapp-api, in both cache-from and cache-to.