Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Docker

Docker: Fix a Stale COPY Layer After Editing .dockerignore

- 16.11.26 - ErcanOPAK

🐳 Docker: Fix a Stale COPY Layer After Editing .dockerignore

Docker’s build cache keys a COPY instruction’s layer off the content hash of exactly what the build context sends it – and that hash is computed only from the files .dockerignore let through the moment the layer was first cached. Editing .dockerignore to newly exclude a file doesn’t retroactively change an already-cached layer’s hash; it only changes what gets sent on a future build. If nothing else that COPY instruction depends on changed, Docker can still report a cache hit and reuse the old layer, silently keeping a file in the image that you just told it to exclude.

🔎 The Problem

# .dockerignore (just edited to exclude secrets/)
secrets/
node_modules/

$ docker build -t app .
=> CACHED [3/6] COPY . .          # cache HIT, nothing recomputed
# secrets/ was present when that layer was LAST cached; editing
# .dockerignore afterward doesn't invalidate that cached hash -
# secrets/ may still be inside the already-cached image layer

✅ Fix: Rebuild Without Cache After Any .dockerignore Change

  • Rebuild with docker build --no-cache after any .dockerignore change you expect to affect the image’s contents, rather than assuming a normal build will notice the ignore-file edit on its own.
  • Verify what actually made it into the image with docker run --rm <image> ls -la <path> instead of trusting that a successful build means the exclusion took effect.
  • Treat .dockerignore itself as a build dependency in CI, so a change to it automatically forces --no-cache, rather than relying on developers to remember it’s a file the cache doesn’t watch.

🚨 Why This Is Dangerous

  • Believing a secrets file is excluded from the image because you added it to .dockerignore, without confirming the layer actually rebuilt, can ship that file inside an already-cached and now-pushed image.

Docker’s cache watches the files it’s told to send – not the file that tells it which files to send. Rebuild without cache whenever that second file changes.

— DevOps Engineer

Related posts:

Docker: Fix docker-compose down -v Leaving Old Data Behind Because a Renamed Project Directory Chang...

Docker: Fix docker-compose down -v Leaving Old Data Behind Because a Renamed Project Directory Chang...

Docker: Fix a Multi-Stage Build That Silently Reuses a Stale Layer Because a COPY Instruction's Sour...

Docker: Fix a Multi-Stage Build That Silently Reuses a Stale Layer Because a COPY Instruction's Sour...

Docker: Manage Container Lifecycle — Start, Stop, Restart

Docker: Manage Container Lifecycle — Start, Stop, Restart

Views: 2

Post navigation

Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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