BuildKit Features (Cache Mounts, Secret Mounts, SSH Mounts)
Speed builds and keep secrets out of layers with BuildKit mount types.
In plain words
Use the mount syntax to stop credentials and repeated compiler/package state from becoming image layers.
Think of it like…
A workbench has a shared tool drawer, a one-time safe deposit box and a borrowed key ring: each is available during the job but none becomes part of the product crate.
Where you’ve seen it
In practice, RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts is the concrete move for this job: speed builds and keep secrets out of layers with BuildKit mount types. The release is not accepted until build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path confirms the result.
Words you’ll hear
- Cache Mount
- The implementation control in
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scriptsfor speed builds and keep secrets out of layers with BuildKit mount types. - Secret Mount
- The result to observe with
build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path, rather than infer from a successful command alone. - SSH Mount
- The production boundary exposed when writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.
Visual diagram
Theory & deep dive
Cache Mount control path
The concrete mechanism is RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts. The observable result is build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path; if those disagree, inspect the runtime evidence before changing unrelated settings.
Speed builds and keep secrets out of layers with BuildKit mount types. Keep Cache Mount, Secret Mount and SSH Mount in the same deployment record so the intent and the observed state cannot drift apart.
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts
build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path, not with command success alone.SSH Mount failure boundary
A secret copied or read through ENV and then deleted is still recoverable from history. SSH forwarding must scope access to the build and avoid placing private keys in the context; cache mount contents need their own invalidation and concurrency model. The narrow failure to design for is writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.
Accept this compromise explicitly: Mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration. The controlled check-build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path-also identifies the point where a different design is safer.
Exercise this failure safely: writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A secret copied or read through ENV and then deleted is still recoverable from history. SSH forwarding must scope access to the build and avoid placing private keys in the context; cache mount contents need their own invalidation and concurrency model.
- Intent
- Cache Mount
When to use it & trade-offs
What it is
BuildKit mounts expose cache, secret or SSH-agent data to one RUN without committing it as a normal image filesystem layer. Cache mounts retain package downloads, secret mounts provide a file for one instruction, and SSH mounts forward an agent socket for authenticated Git access.
Why it matters: A secret copied or read through ENV and then deleted is still recoverable from history. SSH forwarding must scope access to the build and avoid placing private keys in the context; cache mount contents need their own invalidation and concurrency model.
When to reach for it
- The fit is strongest for this job: speed builds and keep secrets out of layers with BuildKit mount types. Pair it with
build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token pathin the runbook. - Production Cache Mount changes should make Secret Mount observable through
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts, rather than trusting the runtime default. - Keep a rollback example for the case where writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.
When to avoid or be careful
- Do not accept command success while the system can still fail by writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.
- Avoid erasing SSH Mount protection merely to dodge this cost: “Mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration”.
- Do not replace the evidence path-
build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path-with a screenshot or a mutable tag.
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts: practical choices
Strengths: BuildKit mounts expose cache, secret or SSH-agent data to one RUN without committing it as a normal image filesystem layer. Cache mounts retain package downloads, secret mounts provide a file for one instruction, and SSH mounts forward an agent socket for authenticated Git access.
Watch out: Mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration.
Strengths: It can begin with RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts and fewer surrounding controls.
Watch out: It silently accepts writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated, which makes the result hard to reproduce after promotion.
Strengths: A secret copied or read through ENV and then deleted is still recoverable from history. SSH forwarding must scope access to the build and avoid placing private keys in the context; cache mount contents need their own invalidation and concurrency model.
Watch out: The platform adds an ownership and debugging layer; retain build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path as the direct check.
Interview signals & numbers to know
When this topic comes up
- RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts
- build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path
- Failure signature: writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated
- Trade-off: Mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration.
Benchmarks & rules of thumb
- 1 RUN scope
- secret and SSH mounts are ephemeral to that instruction
- 3 mount types
- cache, secret and ssh
- 0 secret bytes
- should remain in committed filesystem layers
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scriptsas proof and skippingbuild with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path. - Writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated
- Optimizing Cache Mount without accepting that mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration.
Likely interviewer follow-ups
- Why does
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scriptsaddress Cache Mount here rather than through application code? - How would you distinguish healthy Secret Mount from the failure “writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated” using
build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path? - When would this trade-off-“Mounts eliminate layer leaks and improve speed but require syntax-directive-aware tooling and secure frontend-to-agent configuration”-justify a different SSH Mount design?
RUN --mount=type=cache,target=/root/.npm --mount=type=secret,id=npmrc,target=/root/.npmrc npm ci --ignore-scripts-and one proof-build with --secret id=npmrc,src=.npmrc and inspect the final image history and filesystem for the token path. Then name the boundary: writing the secret with COPY before the private install, or mounting a shared cache with assumptions that one build’s malformed content is isolated.Knowledge check
Put it into practice
Practice related interview Q&A
Apply BuildKit Features (Cache Mounts, Secret Mounts, SSH Mounts) with generative AI interview Q&A and hands-on practice.