What are Docker layer caching strategies in CI?
AdvancedConceptBuild Tools and Artifact Repositories
Answer
Fresh runners have empty Docker caches, so you must export and import the cache. With BuildKit: --cache-to/--cache-from type=registry,ref=...:buildcache,mode=max stores layers in a registry (works anywhere), type=gha uses the GitHub cache, and type=local uses a directory you cache yourself. Order the Dockerfile so rarely changing layers (dependency manifests and install) come before frequently changing ones (source copy).
docker buildx build \
--cache-from type=registry,ref=registry.example.com/api:buildcache \
--cache-to type=registry,ref=registry.example.com/api:buildcache,mode=max \
-t registry.example.com/api:$GIT_SHA --push .
More on Build Tools and Artifact Repositories
What is Maven and what is its build lifecycle?BeginnerWhat is the difference between `mvn package`, `install` and `deploy`?BeginnerHow do Maven dependency scopes and transitive dependencies work?IntermediateWhat is `settings.xml` in Maven and how do CI builds use it?IntermediateWhat is the Maven Wrapper and why use it?BeginnerHow do you cache dependencies and speed up Maven builds in CI?Intermediate