How to Cache Docker Layers With the buildx gha Backend
buildx can read and write BuildKit layer cache to the GitHub Actions cache service via type=gha.
With docker/build-push-action, set cache-from: type=gha and cache-to: type=gha,mode=max. BuildKit then stores and restores layer cache through the same backend that actions/cache uses.
Steps
- Set up buildx with
docker/setup-buildx-action. - Add
cache-from: type=ghaandcache-to: type=gha,mode=max. - Use
mode=maxto also cache intermediate stage 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
cache-from: type=gha
cache-to: type=gha,mode=maxGotchas
- The gha backend counts against the same 10 GB repo cache budget as other caches.
mode=mincaches only the final image layers, which restores less but stays smaller.
Related guides
How to Cache Docker Layers to a Registry With buildxStore Docker BuildKit layer cache in a container registry with cache-from and cache-to type=registry, sharing…
How to Cache Docker Layers to a Local Path With buildxExport BuildKit layer cache to a local directory with cache-to type=local, then pair it with actions/cache to…