Skip to content
Latchkey

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=max

Tips

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →