Interview Prep/Docker/Multi-stage builds, cache, tags and registries

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 →

  1. 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 .
  1. On GitHub Actions use type=gha.
  2. 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.

All Docker questions Roadmap: Docker & containers

More on Multi-stage builds, cache, tags and registries