← Back to all fixes

Docker build using stale cache after code change

The Issue

You changed app code, rebuilt the image, but the running container still serves old behaviour.

Root Cause

Docker caches each layer. If `COPY` comes before `RUN npm install`, any code change busts all layers. But if `COPY . .` is too early and your Dockerfile is ordered badly, you may be copying stale files or running with an old base.

How to Fix
  1. Order Dockerfile correctly: COPY package*.json first, RUN npm install, then COPY . . — so code changes only bust the last layer
  2. Force a full rebuild when needed: `docker build --no-cache -t myapp .`
  3. Confirm the new image is being used: `docker inspect <container> | grep Image`
  4. Make sure you stopped the old container before running the new one