patternbashModerate
Docker layer caching in GitHub Actions with cache-from and cache-to
Viewed 0 times
docker/build-push-action@v5, docker/setup-buildx-action@v3
dockerlayer cachebuildkitbuild-push-actiongharegistry cache
Problem
Every CI run rebuilds Docker images from scratch, wasting 3-10 minutes pulling base images and reinstalling dependencies. Without layer caching, even a one-line change triggers a full rebuild.
Solution
Use BuildKit's inline cache or registry cache with docker/build-push-action:
For registry-based caching (better for self-hosted runners):
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v5
with:
context: .
push: false
tags: myapp:latest
cache-from: type=gha
cache-to: type=gha,mode=maxFor registry-based caching (better for self-hosted runners):
- uses: docker/build-push-action@v5
with:
cache-from: type=registry,ref=ghcr.io/org/myapp:cache
cache-to: type=registry,ref=ghcr.io/org/myapp:cache,mode=maxWhy
BuildKit stores individual layer blobs. mode=max caches intermediate layers too, not just the final image. The gha backend uses GitHub's cache API; registry backend survives runner restarts and works across forks.
Gotchas
- type=gha shares the Actions cache 10 GB quota with other caches in the repo
- mode=max increases cache size significantly—monitor storage usage
- Registry cache requires authentication before the build step
- DOCKER_BUILDKIT=1 is required for non-Buildx builds; docker/setup-buildx-action handles this automatically
Revisions (0)
No revisions yet.