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

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.

All Docker questions Roadmap: Docker & containers

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