Docker caches every layer. On a rebuild it reuses a layer if the instruction is the same and everything before it was reused — for COPY, that includes the files being identical. The first step that changes busts the cache for every step after it.
This Dockerfile copies all the code, then runs npm install. So any edit to any file — one line in server.js — invalidates the COPY, and npm reinstalls every dependency. Slow.
Copy package.json alone first, install, then copy the rest. Now code edits only rebuild the last cheap layer; dependencies come from cache until package.json itself changes.
Try it
Fix the order and rebuild. Then edit server.js (echo '// tweak' >> server.js) and rebuild again — watch RUN npm install say CACHED.