Monk CIMonk CI home

27 Nov, 2026Nitin Mandale

Docker Cache Backends in GitHub Actions: gha, Registry and Persistent BuildKit State


Your Docker build hits the cache locally, but the next CI runner starts from scratch. The reason is simple: BuildKit's internal cache belongs to its builder. When that builder disappears, its local state may disappear too.

For repeatable CI builds, you can export cache to GitHub Actions or a registry, or keep BuildKit state alive between jobs. Each choice trades setup and storage against cache transfer time.

GitHub Actions cache: convenient for GitHub workflows

The gha backend imports and exports build cache through the GitHub Actions cache service. With docker/build-push-action, configure the build step:

cache-from: type=gha,scope=app
cache-to: type=gha,scope=app,mode=max

Use a separate scope per image: the default scope is shared, so exports can overwrite one another. GitHub's branch access rules and cache write permissions also apply. Cache lookups and transfers add time, and API throttling, eviction, or storage limits can turn a warm build into a slower or cold one. Docker currently labels this backend experimental.

In concurrent workflows, decide which jobs can publish to each scope and which only import. A cache that is overwritten by competing builds may lose useful entries even though both builds succeed.

Registry cache: share across environments

A registry cache stores cache separately from your output image. It can be useful when CI and other environments need a shared cache:

cache-from: type=registry,ref=example/app:buildcache
cache-to: type=registry,ref=example/app:buildcache,mode=max

Your builder needs registry access and appropriate credentials. Imports and exports depend on registry throughput and network distance. Avoid having unrelated builds write the same cache reference: an export replaces its previous contents. Use separate references for independent images or branches, and consider importing a trusted default-branch cache as a fallback.

Persistent BuildKit state: reuse the builder's cache

BuildKit already keeps an internal cache. If the builder's state survives between jobs, subsequent builds can reuse it without exporting and importing an external cache each time. For example, Docker's docker-container driver stores state in a Docker volume; that volume must itself survive the runner's lifecycle.

This approach needs storage, cleanup, isolation, and a plan for concurrent jobs. Cache can still be cold after eviction or builder replacement. BuildKit garbage collection controls how old or oversized state is reclaimed.

Choose by measurement

Compare the same commit, builder configuration, runner size, and cache state. Record cold and warm build times, cache import/export time, hit rates, and behavior when several jobs run together. mode=max exports intermediate build results as well as final-stage results, potentially increasing reuse and transfer size. Cache mounts are separate from exported build cache.

Protect cache storage like other build infrastructure. Use BuildKit secrets instead of copying credentials into build layers, and limit who can write cache consumed by trusted builds.

Monk CI's Docker build guide describes a persistent BuildKit state approach. Try it on a representative workflow and compare the full job time, including cache handling, against your current setup.

Try Monk CI for free

Continue with GitHub