Build caches in CI
CI runners start with an empty build cache. Export it with --cache-to and import it with --cache-from: registry, gha, inline and local backends, min vs max.
Cache mounts and build secretsLayer caching and instruction order
Build caches in CI
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A hosted CI runner is a fresh machine, so its BuildKit cache is empty and every build starts from scratch unless you import a cache from somewhere.
- --cache-to exports the build cache to a backend and --cache-from imports it. Both must be set explicitly, and the same location is overwritten on every export.
- mode=min, the default, exports only the layers of the final image. mode=max also exports intermediate stages, which is what makes multi-stage builds hit.
- The registry backend stores the cache as a separate image. The gha backend uses GitHub's Actions cache, which is size-limited and needs Buildx 0.21 or later since the v1 API shut down.
- Inline cache embeds metadata in the pushed image itself. It is the simplest, but supports only min mode.
- Cache mounts are not exported with --cache-to, so package-manager caches need a workaround or a persistent builder.
- Read from the main branch's cache on pull requests, but only write from trusted pushes, so untrusted code cannot poison it.
Forty seconds on a laptop, nine minutes in CI
The same commit builds in 40 seconds on your machine and nine minutes in the pipeline. The Dockerfile is not the problem. Your laptop’s builder has hours of cache from earlier builds. A hosted CI runner is a fresh virtual machine that is thrown away after the job, so its BuildKit cache is empty every single time. Perfect instruction order does nothing when there is nothing to hit.
The fix is to store the cache somewhere that outlives the runner, then load it at the start of the next build.
Export and import
Two flags do it:
docker buildx build --push -t registry.example.com/notes:$GIT_SHA \
--cache-to type=registry,ref=registry.example.com/notes:buildcache,mode=max \
--cache-from type=registry,ref=registry.example.com/notes:buildcache .
--cache-to exports the cache after the build and --cache-from imports it before. The backends page is explicit that, unlike the builder’s local cache, external caches are never used unless you set both. It also warns that each export overwrites its location, so per-branch caches need different refs or scopes.
--cache-from can be repeated. A common pattern imports the branch’s own cache and falls back to main’s, so the first build of a new branch is not cold.
min or max
mode is the setting that matters most for multi-stage builds.
mode=min, the default, exports only layers that are in the final image.mode=maxexports every layer, including intermediate stages.
In a multi-stage build, the expensive work is usually in a stage that is not shipped: the compile, the npm ci in the builder stage. With min those layers are never exported, so CI rebuilds them every time while the cheap final stage hits. max is bigger and slower to upload, and almost always worth it.
The backends
| Backend | Stores the cache in | Notes from the docs |
|---|---|---|
registry |
A separate image, such as :buildcache |
Any OCI registry. Supports max. The usual choice. |
gha |
GitHub’s Actions cache | Recommended inside GitHub Actions, within GitHub’s size limits. Use scope per image. |
inline |
The pushed image’s own config | Simplest, but min only and mixes cache with the artifact. |
local |
A directory, OCI layout | Pair with actions/cache or a shared disk. |
s3, azblob |
Object storage | Experimental. |
The default docker driver supports inline, local, registry and gha only with the containerd image store. Otherwise use a docker-container builder, which is what docker/setup-buildx-action creates.
Two gha details from the docs that bite in practice. GitHub shut down the v1 cache API on 15 April 2025, so you need Buildx 0.21 or later, BuildKit 0.20 or later, and Docker Engine 28 or later if you build with the docker driver. And the default scope is buildkit, so two images built in one repository overwrite each other’s cache until you give each its own scope.
A workflow
A GitHub Actions job that builds on every pull request and pushes from main, with a registry cache that only trusted pushes write:
name: image
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: docker/setup-buildx-action@v4
- uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v7
with:
push: ${{ github.event_name == 'push' }}
tags: ghcr.io/example/notes:${{ github.sha }}
cache-from: type=registry,ref=ghcr.io/example/notes:buildcache
cache-to: ${{ github.event_name == 'push' && 'type=registry,ref=ghcr.io/example/notes:buildcache,mode=max' || '' }}
Pull requests read main’s cache and get fast builds. Only pushes to main write it. That split is the defence against cache poisoning: a cache entry is a layer that later trusted builds will reuse without re-running the step, so whoever can write the cache can put files into your releases. The GitHub Actions cache page notes GitHub now gives some untrusted triggers, such as pull_request_target, read-only cache access by default for the same reason.
What exported cache does not cover
Cache mounts live in the builder’s local storage. The Dockerfile reference says they are not included in --cache-to and not restored by --cache-from. A fresh runner starts with them empty. The docs point to reproducible-containers/buildkit-cache-dance, which copies mount contents in and out of the Actions cache. A persistent builder, self-hosted or Docker Build Cloud, keeps them naturally.
Caching also has a freshness cost. A cached RUN apt-get upgrade never upgrades anything. A scheduled nightly build with --pull --no-cache, and no cache import, picks up base image and package security fixes that day-to-day cached builds would skip.
About the video
The embed is SysEng Quick’s “Caching Docker Layers with GitHub Actions”, April 2024, 18.5 minutes. Its chapters cover why caching matters, implementing the gha cache, testing it with code changes, a limitation with how the cache interacts with tags, a tagging workaround, waiting for cache builds, and the registry cache method. It is from a small channel and predates the GitHub cache v2 migration and the read-only cache policy for untrusted triggers, both covered above. English was judged from the title, the description and the English caption track. anthonywritescode’s video in deeper explains --cache-from from first principles.
What was run: the workflow above was parsed with the yaml library to check its structure. It was not run on GitHub. The backends, modes, version requirements and write restrictions are from the cache backends pages and the GitHub Actions cache page in the docker/docs repository, and the action versions from that repository’s site config, checked 2026-11-28.
Where this fits
Builds are fast everywhere. The last lesson of the group is about what you ship rather than how fast: shrinking images.
Check yourself
A multi-stage build uses --cache-to type=registry,ref=...:buildcache with no mode, and the build stage always rebuilds in CI. Why?
The default mode=min only exports layers that end up in the final image. The build stage's layers were never exported. Use mode=max.
Two images in the same repository both use cache-to type=gha. Each build seems to lose the other's cache. What is wrong?
Both use the default scope, buildkit, so each export overwrites the other. Give each image its own scope, for example scope=api and scope=worker.
Your Go build uses RUN --mount=type=cache for the module cache, and CI imports a max-mode registry cache. Modules still download every run when go.mod changes. Why?
Cache mount contents live in the builder's local storage and are not part of exported cache. Only layers are. Use a persistent builder or a workaround such as buildkit-cache-dance.
Why should pull request builds from forks import the cache but not export it?
Exporting lets the pull request's code write layers that later trusted builds would reuse, which is cache poisoning. Read-only import gives forks the speed without the write access.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.