How to cache Docker layers on Google Cloud Build using BuildKit and Artifact Registry

Google Cloud Build runs on ephemeral virtual machines, meaning every CI/CD build starts fresh with no Docker layer cache from previous runs. The solution is to externalize the cache to Artifact Registry, a shared registry accessible by all builds. Using BuildKit with cache-mode=max allows unchanged Docker layers — such as resolved Python dependencies — to be reused across builds without re-downloading packages. The approach follows the principle of copying rarely changed files like dependency manifests before frequently changed source code, maximizing cache hits. This technique is demonstrated using a Python FastAPI app managed with the uv package manager, and is the first in a series covering Docker caching strategies on Cloud Build.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in