quickstart →

Docker layer caching in GitHub Actions with Buildx (type=gha, registry)

Speed up Docker builds in GitHub Actions by caching layers with Buildx: the gha, registry, inline and local cache backends compared, with YAML examples.

To cache Docker layers in GitHub Actions, build with Buildx through docker/build-push-action and point its cache-from and cache-to parameters at a cache that outlives the runner. Four Buildx cache backends work for this: the GitHub Actions cache (type=gha), a container registry (type=registry), the image you push (type=inline) and a local directory that actions/cache saves (type=local). For most CI, start with cache-from: type=gha and cache-to: type=gha,mode=max.

Caching your ~/.npm or ~/.cache/pip directory, as in Caching dependencies, does nothing for docker build. Image layers live in the Docker engine’s own storage, which starts empty on every fresh runner, so without an external cache every RUN apt-get install ... and every COPY . . runs again on every build. This article walks through each backend with runnable YAML.

Why Docker builds are slow in CI#

On your laptop, the second docker build is fast because the engine still has the layers from the first build sitting in its local cache. CI runners are different: each job runs on a clean, ephemeral machine, so the layer cache is always cold. Buildx rebuilds the full Dockerfile from layer zero, redownloading base images and re-running every instruction.

To carry layers from one run to the next you need an external cache that survives the runner being torn down. Buildx supports several such backends, and you wire them in through two parameters: cache-from (where to import layers) and cache-to (where to export them).

Setting up Buildx and the build-push-action#

The starting point for all the examples below is the same: set up Buildx (a Docker CLI plugin built on BuildKit) with docker/setup-buildx-action ↗, then build with docker/build-push-action ↗. If you build images for an architecture other than the runner’s own (for example linux/arm64 on an amd64 runner), also add docker/setup-qemu-action ↗ for emulation. Emulated builds are much slower than native ones; with arm64 runners available (ubuntu-24.04-arm on GitHub, any Graviton type on RunsOn), build each platform natively in a matrix instead.

.github/workflows/ci.yml
name: Docker build
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build
uses: docker/build-push-action@v7
with:
context: .
push: false
tags: my-app:latest

That builds the image but caches nothing across runs. Everything that follows is about filling in cache-from and cache-to.

GitHub Actions cache: type=gha#

The most convenient backend exports layers into the same cache service that actions/cache uses. It needs no extra credentials and no registry: docker/build-push-action (or docker/bake-action) passes the runner’s cache URL and token to Buildx. A plain run: docker buildx build --cache-to type=gha ... step doesn’t get them, and Buildx then skips the type=gha cache without any error. Run crazy-max/ghaction-github-runtime@v4 ↗ before such a step to expose them.

.github/workflows/ci.yml
- name: Build with GHA cache
uses: docker/build-push-action@v7
with:
context: .
push: false
tags: my-app:latest
cache-from: type=gha
cache-to: type=gha,mode=max

The interesting part is mode:

  • mode=min (the default) caches only the layers that end up in the final image.
  • mode=max caches all layers, including those of intermediate stages that never reach the final image. That uses more storage, but in a multi-stage build it gives you far more cache hits: when your source changes, the builder stage’s dependency install is still a hit. For CI, mode=max is almost always what you want.

A few things to keep in mind:

  • This backend draws from the same Actions cache as your dependency caches, so a big mode=max export can push them out once the repository goes over its cache limit. If a spending budget for cache storage runs out, the cache turns read-only and cache-to exports fail.
  • Since January 2026, GitHub accepts at most 200 cache uploads per minute per repository. Each new layer is uploaded as a separate cache entry, so a mode=max export with many layers, or a wide matrix exporting at the same time, can hit that limit and fail.
  • If a build fails with This legacy service is shutting down, it runs a Buildx older than 0.21 or a BuildKit older than 0.20 (an old install on a self-hosted runner, or a pin in docker/setup-buildx-action’s version or driver-opts: image=moby/buildkit:...), which still calls the cache service GitHub shut down in April 2025: update it.
  • Each build writes to the buildkit scope by default, so two images built in the same repository (or the jobs of a matrix) overwrite each other’s cache. Give each one its own scope: cache-from: type=gha,scope=api and cache-to: type=gha,mode=max,scope=api.
  • The cache follows the same branch scope and write rules as actions/cache: since June 2026, pull_request_target, issue_comment and workflow_run runs (and a merged pull request’s closed run) can only read the default branch’s cache, unless the workflow sets cache-mode: write. Unlike actions/cache, which only logs a warning, a refused cache-to: type=gha export fails the build step with error writing layer blob: failed to reserve cache, even after the image was pushed. In those workflows keep cache-from: type=gha and drop cache-to, or use cache-to: type=gha,mode=max,ignore-error=true, and let a push workflow on main fill the cache.

Registry cache: type=registry#

Instead of the Actions cache, you can push the cache to a container registry as a dedicated image tag: cache-to: type=registry,ref=... writes it and cache-from with the same ref reads it back. This survives independently of the Actions cache limits and can be shared across branches and even repositories that have pull access to the registry.

It needs a registry login first, via docker/login-action ↗, and a token that can push to the registry:

.github/workflows/ci.yml
permissions:
contents: read
packages: write # push the image and :buildcache to ghcr.io
steps:
# ...checkout and setup steps from the first example
- name: Log in to registry
uses: docker/login-action@v4
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build with registry cache
uses: docker/build-push-action@v7
with:
context: .
push: true
tags: ghcr.io/my-org/my-app:latest
cache-from: type=registry,ref=ghcr.io/my-org/my-app:buildcache
cache-to: type=registry,ref=ghcr.io/my-org/my-app:buildcache,mode=max

Pushing to ghcr.io with GITHUB_TOKEN needs packages: write. If the package already exists and wasn’t published from this repository, also give the repository write access under Manage Actions access in the package settings. Pull requests from forks get a read-only token by default, so skip push and cache-to there.

The ref points at a separate tag (:buildcache) so the cache manifest doesn’t clobber your real image tags. mode=max works here too. The trade-off versus type=gha is the transfer to and from the registry, plus storage on registries that bill for it, such as Amazon ECR. As of November 2026, ghcr.io storage and bandwidth are free. In return, you get a cache that any job in your organization can reuse.

Unlike type=gha, a registry cache has no branch scope. Every run that exports to :buildcache replaces it, so a feature-branch or pull request build overwrites the cache main uses, and any workflow allowed to push the package can poison it. Export only from pushes to main, and import everywhere:

cache-from: type=registry,ref=ghcr.io/my-org/my-app:buildcache
cache-to: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && 'type=registry,ref=ghcr.io/my-org/my-app:buildcache,mode=max' || '' }}

The github.event_name check keeps out pull_request_target, issue_comment and workflow_run runs, which also have the default branch as github.ref. To keep a cache per branch instead, export each branch to a tag of its own and list :buildcache as a second cache-from line, so a new branch starts from main’s layers.

Inline cache: type=inline#

The simplest possible option: embed the cache metadata directly inside the image you’re already pushing. There’s no separate cache artifact to manage at all. Like the registry example, it pushes to ghcr.io, so the job needs the same login step and packages: write.

.github/workflows/ci.yml
- name: Build with inline cache
uses: docker/build-push-action@v7
with:
context: .
push: true
tags: ghcr.io/my-org/my-app:latest
cache-from: type=registry,ref=ghcr.io/my-org/my-app:latest
cache-to: type=inline

The catch: inline cache is mode=min only — it can’t store intermediate layers, because they aren’t part of the pushed image. That makes it a poor fit for multi-stage builds where the expensive work happens in a stage that gets discarded. Reach for it when your build is single-stage and you want zero extra moving parts.

Local cache: type=local#

The local backend writes the cache to a directory on disk. On its own that’s useless in CI, because the disk disappears with the runner — so you pair it with actions/cache to persist that directory between runs.

By default the local exporter keeps the blobs of every earlier export, so the directory, and your actions/cache entry, grows run after run. Buildx 0.35.0 and later accept reset=true, which deletes the blobs no tag references, so you can export back into the same directory. As of November 2026, GitHub’s Ubuntu images ship Buildx 0.38.

.github/workflows/ci.yml
- name: Cache Docker layers
uses: actions/cache@v6
with:
path: ${{ runner.temp }}/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build with local cache
uses: docker/build-push-action@v7
with:
context: .
push: false
tags: my-app:latest
cache-from: type=local,src=${{ runner.temp }}/.buildx-cache
cache-to: type=local,dest=${{ runner.temp }}/.buildx-cache,mode=max,reset=true

On an older Buildx, the usual workaround is to export to a separate -new directory, then delete the old directory and move the new one into its place in a last step, before the cache is saved.

This is still more work than type=gha (two coordinated steps instead of one), and it uses the same Actions cache. With a key per commit, every new commit also saves a full copy of the directory as a new cache entry, while type=gha stores each layer once, so type=local fills the free 10 GB faster. Most teams are better served by type=gha or type=registry. It’s worth knowing because you’ll see it all over older workflows and blog posts.

Make the layers cacheable#

A backend stores the layers, but your Dockerfile decides whether a build can reuse them. BuildKit reuses a layer only when its instruction and the files it depends on are unchanged, and once a layer is rebuilt, the steps that follow it are rebuilt too. Whichever backend you pick:

  • Install dependencies before you copy the source. With COPY . . before the install step, a change to any file reinstalls every dependency. Copy the package files first, install, then copy the rest:

    Dockerfile
    COPY package.json package-lock.json ./
    RUN npm ci
    COPY . .
    RUN npm run build
  • Keep the build context small. Add a .dockerignore file that leaves out node_modules, build output and anything else the image doesn’t need. Otherwise a change to one of those files invalidates COPY . . and every step after it.

  • Cache mounts start empty on a fresh runner. A RUN --mount=type=cache mount keeps a package manager’s download cache between builds on the same builder, but no backend exports or imports it. To keep it between runs, pair actions/cache with reproducible-containers/buildkit-cache-dance ↗, which copies the mount’s contents out of the builder after the build and back in on the next run.

Which backend should you use?#

A quick decision guide:

  • type=gha — your default. Zero setup with docker/build-push-action, great for a single repo’s CI. Watch the free 10 GB per repository.
  • type=registry — when you want to share layers across repositories or branches, or your cache outgrows the free 10 GB of Actions cache. Needs a registry login.
  • type=inline — only for simple single-stage builds where you want the absolute minimum configuration and don’t need intermediate layers.
  • type=local — rarely the best choice today; useful if you specifically need cache on disk. Export with reset=true (Buildx 0.35 and later), or the directory grows with every run.

Conclusion#

Docker builds are slow in CI for one reason: the layer cache is cold on every run. Dependency caching won’t help — you need to point Buildx at an external cache with cache-from and cache-to. Start with type=gha,mode=max for its near-zero setup, graduate to type=registry when you need to share layers or outgrow the free 10 GB of Actions cache, keep type=inline in your back pocket for the simplest builds, and reach for type=local only when you have a specific reason to. Pick the backend that matches how your images are built and shared, and your CI pipeline will spend its time shipping instead of rebuilding the same layers over and over.

On RunsOn#

RunsOn’s Magic Cache serves type=gha from an S3 bucket in your AWS account, with no 10 GB cap, and your cache-from and cache-to lines stay as they are. Entries expire after the stack’s retention window, 10 days by default. Magic Cache is opt-in: on Flex, add extras=s3-cache to the job label and run runs-on/action@v2 before the build; on Fleet, the platform team enables the s3-cache extra in the runner definition.

.github/workflows/ci.yml
jobs:
build:
runs-on: runs-on=${{ github.run_id }}/runner=2cpu-linux-x64/extras=s3-cache
steps:
- uses: runs-on/action@v2
- uses: actions/checkout@v7
# ...the Buildx steps above, unchanged

For type=registry, use the stack’s ephemeral ECR registry once it is enabled: extras=ecr-cache in the job label (on Fleet, the ecr-cache extra in the runner definition) logs the job in to it, so you need no login step. Give the cache a tag of its own, such as ${{ env.RUNS_ON_ECR_CACHE }}:my-app-cache. For very large layer sets, sticky disks keep BuildKit’s state on EBS, so there is nothing to export or import. Our PostHog benchmark compares the three with GitHub’s cache on one build.