CI rebuilds from scratch every run and takes 12 minutes. How do you bring it down?
IntermediateScenarioMulti-stage builds, cache, tags and registries
Answer
Situation read → Ephemeral CI runners have no local layer cache.
What I'd do →
- Use BuildKit remote cache:
docker buildx build \
--cache-from type=registry,ref=reg.example.com/app:buildcache \
--cache-to type=registry,ref=reg.example.com/app:buildcache,mode=max \
-t reg.example.com/app:$SHA --push .
- On GitHub Actions use
type=gha. - Reorder the Dockerfile so deps are above source.
Why → mode=max also exports intermediate stages, so multi-stage builds get cache hits.
Interview tip → Say you track cache hit rate from the CACHED lines in build output.
More on Multi-stage builds, cache, tags and registries
Show a multi-stage Dockerfile for a Java Spring Boot app and explain what you gain.BeginnerYou tag every build `myapp:latest`. A rollback is requested. What happens?BeginnerHow do you promote an image from dev to prod registry without rebuilding?IntermediateDisk on the build server keeps filling up with images.BeginnerWhen do you use `--no-cache` and when is it a smell?BeginnerExplain a build where `COPY` always misses the cache even though code did not change.Intermediate