Skip to content

The Docker layer cache

Turn it on with one line. A container registry beside the runners that BuildKit pushes layers to, so an unchanged layer is not rebuilt on the next job.

Last updated

Building an image in CI usually means rebuilding every layer, because the builder starts empty on every job. The layer cache gives BuildKit somewhere to put those layers that survives between jobs.

It is a container registry we run beside the runners, reachable from a job and from nowhere else. Layers are addressed by digest, so a layer that has not changed is not rebuilt and not re-uploaded.

Unlike the Actions cache, this one is metered by what it stores. The pricing page has the rate.

Add one step before your build:

yaml
- uses: runners-io/setup-docker-builder@v1

Every docker build, docker buildx build and docker/build-push-action step after it in the same job reads its layers from the cache and writes new ones back. Nothing else in the workflow changes: no flags, no hostname, no credential.

yaml
jobs:
  image:
    runs-on: runnersio-4vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v4
      - uses: runners-io/setup-docker-builder@v1
      - run: docker build -t app .
      - run: docker run --rm app ./run-tests

The second time that job runs, the layers that have not changed come from the cache instead of being built again.

If you move a repository with the migration pull request, the review step offers this line for each workflow file that builds with docker/build-push-action or docker/bake-action, unticked. Tick it for a file and the pull request adds the step directly before the build in the jobs that move; leave it unticked and nothing about your builds changes. It is added once per job, never to a job that already has a setup-docker-builder step, and never to a job that stays on another runner. To turn it off later, delete the line.

Two kinds of job are offered with a note beside the box:

  • A matrix job (runs-on: ${{ matrix.os }} over a list written out in the job) is offered when at least one leg moved to a runners.io runner. The step is added once, and it does nothing on a leg that stays on GitHub, such as a Windows or macOS leg. A matrix built by an expression is left alone, as the runner change is.

  • A job that only uses docker/bake-action gets the step before the first bake. Whether your bake file already sets its own cache is in the HCL file, which the pull request does not read, so it cannot tell; the step is added anyway and does no harm where the file caches itself. A job that also has a docker/build-push-action is judged by that action alone.

  • It checks the cache is there. The job must have been offered one, hold a credential for it, and get an answer from it. If any of that is missing, the step says which and changes nothing.

  • It makes sure the builder can export a cache. On our runners that is the default builder, which both caches to our registry and sees images an earlier step in the same job built — so a build FROM an image you just built keeps working and is cached. (On an older Docker whose image store cannot do both, the step falls back to a container builder; see the note below.)

  • It gives each build its own cache. One per image, worked out from the Dockerfile, the target and the local build context, so two images built in one job do not overwrite each other's.

  • It leaves alone any build that manages its own cache. A build that passes --cache-from or --cache-to, or that runs on another builder, is not changed.

A cache that cannot be reached never fails a build. The build runs without it, and is only slower.

If a build is FROM an image an earlier step in the same job produced, it keeps working. On our runners it is also cached, like any other build. On an older Docker whose image store cannot both cache and see local images, that one build runs without the layer cache (the step says so) rather than failing — every other build in the job still uses the cache.

One case the older-Docker fallback does not catch: an image you pulled and then gave a new name (docker pull node:22, then docker tag node:22 my-base:1) still looks to the step like an image a registry can serve, so a build FROM my-base:1 fails there with "pull access denied". This does not happen on our runners, which use the newer image store. If you meet it, build FROM the original name (FROM node:22), or pass automatic: false and wire the cache yourself.

On GitHub-hosted runners, or anywhere else, the step does nothing and says so in one notice. A workflow that runs in both places can keep it without a condition.

InputDefaultWhat it does
keyone per imageNames the cache. Set it to make builds share one cache that would otherwise get separate ones.
automatictruefalse sets up the builder only, for wiring the cache yourself with the outputs below.
OutputWhat it holds
enabledtrue when the cache was set up, false when the step did nothing.
cache-from, cache-toValues for docker/build-push-action's inputs of the same names. Empty when the step did nothing, which that action reads as no cache.

To wire it yourself:

yaml
- id: layers
  uses: runners-io/setup-docker-builder@v1
  with:
    automatic: false
- uses: docker/build-push-action@v6
  with:
    context: .
    cache-from: ${{ steps.layers.outputs.cache-from }}
    cache-to: ${{ steps.layers.outputs.cache-to }}

docker buildx bake and docker compose build are not changed by the step. Give them the cache-from and cache-to outputs the same way.

The cache belongs to one repository of one GitHub account, and it is keyed by their GitHub ids as well as their names:

  • Two repositories in the same account do not share layers.

  • Two accounts never do.

  • An account or repository name that is freed and registered again by someone else does not inherit the previous owner's layers.

  • Renaming a repository starts it a new, empty cache.

Which cache a request reaches comes from the job's own credential, never from the reference it asks for. A job that names another repository's reference, exactly, reaches its own cache and finds nothing there. We test this on the live service from two separate GitHub accounts: one builds, and the other asks for those layers by their exact reference and by every digest, and is refused each time.

A build looks for layers its own branch wrote first and then the default branch's, and writes only to its own branch. A build started by an event that may not write the cache, such as pull_request_target, reads the default branch's alone and writes nothing. A feature branch never reads a sibling's.

Like the Actions cache, the Docker layer cache keeps what your builds use and deletes what they stop using. We delete a cache tag that no build has read or written for seven days, and then the layers that no cache tag of yours still points to and that no push has written for seven days. If a push would take one of your repositories past your organisation's 10 GiB layer cache, we first delete layers nothing points to, and then the cache tags in your organisation that no build has used for the longest, across all its repositories, never one used in the last two days, together with the layers only they pointed to, until the push fits. The oldest-used images in the organisation go first, whichever repository they are in, so a push to one repository can clear the oldest cache of another. We never delete another organisation's tags to make room. If nothing is old enough, new layers are not stored, and the build carries on without them. Every repository in the organisation counts towards that one 10 GiB.

  • A build uses a tag when it reads the cache or writes it. A build that imports a cache reads its tag, and one that exports a cache writes it. A read counts even when the build writes nothing back, so a cache that is only ever imported is kept for as long as builds import it. A read writes the tag again at most once an hour.

  • A layer a cache tag still points to is kept for as long as the tag is, however old the layer. The tag goes after seven days without use, or earlier when the cache is full, as above. Each build that writes its cache points its branch's tag at that build's layers, so the layers an older build needed and this one does not are the ones that can go.

  • A push writes a layer when it uploads it, and when it relies on one already here: then we write the layer again, at most once a day. A layer no tag points to goes once no push has written it for seven days.

  • Each branch has tags of its own. A branch you stop building, a deleted one included, loses its tags seven days after its last build, and then the layers only they pointed to. So does the old cache of a renamed repository.

  • When a build moves a tag to new layers, the layers it pointed to before are kept for six more hours, so a build that was still reading them can finish.

  • We check about once an hour, so a tag or a layer usually outlasts its seven days by about an hour.

  • To have layers deleted sooner, ask us, as the privacy policy says.

Because the cache is metered by what it stores, the layers it keeps are the layers you pay for, and the ones it deletes stop counting.

It caches layers, not build context and not test fixtures. If your slow step is downloading dependencies rather than building image layers, the Actions cache is the mechanism that helps, and using both is normal.

It does not make an image available to a later job as an image. A job that needs the built image should push it to a registry, or build it again — the layer cache makes that second build fast, but it is not an artefact store.

The Docker layer cache is not offered on Windows. A job on a -windows-2025 or -windows-2022 runner is not given a cache, so a docker build there starts from an empty layer store, and setup-docker-builder does nothing and says so. The reason is the image, not a switch: the cache holds the layers of Linux container images, and Docker Engine on Windows Server Core runs Windows containers only. The runner image page has the rest of what a Windows job does and does not have. actions/cache works on Windows, and is the cache a Windows job has. Sticky disks are not offered on Windows either.

Where the layer cache holds image layers, a sticky disk holds a whole filesystem across jobs for the same repository. Build tools with their own on-disk caches — Bazel, Gradle, cargo, go build — usually want that rather than this.