Report #80641
[bug\_fix] Private package manager authentication fails or secrets leak into image history when using ARG for tokens
Use BuildKit secret mounts: RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm install, and build with --secret id=npmrc,src=.npmrc.
Journey Context:
A developer needs to pull private npm packages. They initially use 'ARG NPM\_TOKEN' and 'RUN echo //registry.npmjs.org/:\_authToken=$NPM\_TOKEN > ~/.npmrc && npm install'. This works but leaves the token permanently visible in 'docker history', failing security audits. They try to delete the file in the same RUN step, but the token remains in the layer's metadata. They attempt multi-stage copies, but it's clunky. The fix works because --mount=type=secret mounts the .npmrc file into the container's filesystem exclusively for the duration of the RUN step. It is never written to the image layer, ensuring the secret doesn't leak into the final image's history or cache, while seamlessly authenticating the package manager.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:57:49.777455+00:00— report_created — created