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 Multi-Stage Build Caching a Stale npm Install Layer

- 17.11.26 - ErcanOPAK

🐳 Docker: Fix a Multi-Stage Build Caching a Stale npm Install Layer

The standard pattern of copying package.json before the rest of the source so npm install can be cached as its own layer only works if every file that actually determines what gets installed is part of that early COPY — and it’s an easy mistake to copy package.json alone while forgetting package-lock.json (or the equivalent yarn.lock / pnpm-lock.yaml), because Docker’s layer cache key is computed purely from the files that were actually copied into that step. The lock file can change — pinning entirely different transitive dependency versions — and the install layer’s cache key never moves, so Docker happily reuses an install from before the lock file update, indefinitely.

🔎 The Problem

FROM node:20 AS build
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build

# package-lock.json changed (different transitive versions pinned),
# but it was never part of the COPY above, so the cache key for
# 'RUN npm install' is unchanged -> Docker reuses the OLD install layer

✅ Fix: Copy Every File the Install Actually Depends On

  • Copy both package.json AND its lock file (package-lock.json, yarn.lock, or pnpm-lock.yaml) before the install step — the cache key has to include every file that can change what gets installed, not just the one most people remember.
  • Prefer npm ci over npm install in the build stage; it fails loudly if the lock file and package.json disagree instead of silently resolving a slightly different tree, which also makes a stale-cache situation easier to notice.
  • When an install looks suspiciously fast for a dependency change you know should matter, rebuild with --no-cache once to confirm whether you’re looking at a genuine result or a cache hit that shouldn’t have happened.

🚨 Why This Is Dangerous

  • Shipping a production image built against dependency versions from weeks ago because the lock file update that was supposed to patch a vulnerable transitive package never actually got installed — the build looked fast and green the whole time, for exactly the wrong reason.

A cache key can only know about the files you showed it — a lock file you forgot to copy might as well not exist.

— DevOps Engineer

Related posts:

Docker: Fix docker compose up --scale Silently Failing to Add Replicas Because a Fixed Host Port Map...

Docker: Fix docker compose up --scale Silently Failing to Add Replicas Because a Fixed Host Port Map...

Docker Build Is Slow Even With Cache

Docker Build Is Slow Even With Cache

Step-by-Step: Docker Multi-Environment Setup (Development, Staging, Production)

Step-by-Step: Docker Multi-Environment Setup (Development, Staging, Production)

Views: 4

Post navigation

Git: Fix a Worktree Losing Track of a Branch After a Rename
Photoshop: Fix a Smart Object Resetting Its Filters After Re-Linking

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,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

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

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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