How to Cache Docker Layers with the GitHub Actions Cache
With Buildx, set cache-from/cache-to to type=gha so Docker stores and restores layers via the GitHub Actions cache.
Buildx can read and write its layer cache through the native GitHub Actions cache backend. Set cache-from: type=gha and cache-to: type=gha,mode=max on docker/build-push-action and unchanged layers are skipped on the next run.
Steps
- Set up Buildx with
docker/setup-buildx-action. - On
docker/build-push-action, addcache-from: type=gha. - Add
cache-to: type=gha,mode=maxto also export intermediate layers.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ci
cache-from: type=gha
cache-to: type=gha,mode=maxGotchas
mode=maxcaches every stage (best reuse) but writes more data;mode=minonly caches the final image layers.- The gha backend shares the same 10 GB per-repo cache budget as
actions/cache, so large layer sets can evict other caches.
Related guides
How to Cache Docker Layers to a Registry in GitHub ActionsCache Docker build layers to a container registry in GitHub Actions with Buildx cache-to type=registry, so la…
How to Cache a Next.js Build in GitHub ActionsCache the Next.js .next/cache directory in GitHub Actions keyed on the lockfile and source, so next build reu…