Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Docker

Docker: Fix a Build Secret Leaking Into a Cached Layer

- 28.11.26 - ErcanOPAK

🐳 Fix a Build Secret Leaking Into a Cached Layer

Docker’s build cache stores every layer by the exact filesystem diff it produced, and that’s true whether a file written during that layer was deleted later in the same RUN instruction or not – the cache is keyed on the instruction, not on the final state of the filesystem. A RUN instruction that writes a secret to a file, uses it, and then removes it in the same line is cached once and reused on every subsequent build whose earlier instructions haven’t changed, which means that cached layer’s diff can still be pulled apart with docker history –no-trunc or docker save to recover the secret, even though the final image’s visible filesystem never shows the file at all.

🔎 The Problem

ARG NPM_TOKEN
RUN echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc \
    && npm install \
    && rm .npmrc
# .npmrc is gone from the final image filesystem - but the build cache
# still holds this exact layer's diff from the last time it ran, and a
# `docker build --cache-from registry/myimage:buildcache` on CI reuses
# it layer-for-layer without ever re-running the RUN instruction

✅ Fix: Use BuildKit’s Secret Mounts

  • Switch the secret from a build ARG to BuildKit’s dedicated mechanism: RUN –mount=type=secret,id=npmtoken cat /run/secrets/npmtoken > .npmrc && npm install, built with docker build –secret id=npmtoken,src=./token.txt – BuildKit mounts the secret into an ephemeral location that never becomes part of any cached layer’s filesystem diff.
  • If a remote build cache was ever populated before the migration, invalidate or rebuild it explicitly (a cache-busting ARG, or simply deleting the old cache image) rather than assuming switching the Dockerfile alone retroactively cleans layers already sitting in a shared registry-backed cache.
  • Rotate any token that was ever baked into a build this way – removing the file from the Dockerfile doesn’t remove it from history that’s already been pushed, pulled, or cached anywhere.

🚨 Why This Is Dangerous

A secret that never appears in docker inspect or the final container’s filesystem can still be sitting in plain sight inside a cached layer, and a shared CI build cache is exactly the kind of place other pipelines, other teams, or a compromised cache backend get to read from.

BuildKit’s secret mounts exist because a RUN instruction’s cache doesn’t know the difference between a file you deleted and a file that was never there – only one of those is actually safe.

— DevOps Engineer

Related posts:

Why Multi-Stage Builds Are About Security, Not Size

Why Multi-Stage Builds Are About Security, Not Size

Docker: Follow Dockerfile Best Practices

Docker: Follow Dockerfile Best Practices

Docker: Fix a Container Stuck in a Tight Restart Loop With Nothing Ever Printed in docker logs Becau...

Docker: Fix a Container Stuck in a Tight Restart Loop With Nothing Ever Printed in docker logs Becau...

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

JavaScript: Fix a Debounced Handler Dropping a Keystroke
The AI Prompt That Rewrites a Regex With Named Groups

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com