How to Cache Docker Layers in GitHub Actions
Rebuilding every Docker layer on every run is slow and wasteful. BuildKit caching reuses what did not change.
Use Buildx with a cache backend so unchanged layers are restored instead of rebuilt.
With the GitHub Actions cache backend
.github/workflows/docker.yml
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
push: true
tags: myimage:latest
cache-from: type=gha
cache-to: type=gha,mode=maxTips
- Order your Dockerfile so rarely-changing layers (deps) come before frequently-changing ones (source).
- Use multi-stage builds and copy only artifacts into the final stage.
- A registry-backed cache (ECR/GHCR) persists across runners better than local cache.
Related guides
Docker "failed to solve" BuildKit Errors - Diagnose and FixDecode Docker BuildKit "ERROR: failed to solve" messages in CI - missing files, bad cache mounts, network fai…
How to Speed Up GitHub Actions: 9 High-Impact TacticsMake GitHub Actions faster: caching, parallelization, test splitting, Docker layer caching, slimmer images, a…
GitHub Actions Workflow Template for Docker (build & push)A ready-to-use GitHub Actions workflow for Docker (build & push) with caching and best practices - copy, comm…