Cache mounts in Dockerfiles: when are they worth it?
IntermediateScenarioMulti-stage builds, cache, tags and registries
Answer
Situation read → Package managers redownload packages on each change of the dependency layer.
What I'd do →
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build go build ./...
Why → The cache directory persists between builds on the same builder but never enters the image, so you get speed without size. It is not shared across CI runners unless the builder is persistent.
Interview tip → On ephemeral CI combine with registry cache or a long-lived buildkit daemon.
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.BeginnerCI rebuilds from scratch every run and takes 12 minutes. How do you bring it down?IntermediateYou 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?Beginner