Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Dockerfile
You change one line in server.js, and Docker reinstalls every npm package. Look at this Dockerfile. The COPY dot dot instruction, which copies your whole project in, comes before RUN npm install. That ordering is the problem, and it's why a tiny edit triggers a slow install.
FROM node:20
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]Build after editing one line of server.js
Read the build log from the top. The first two steps, FROM and WORKDIR, say CACHED, so Docker reuses them. Then COPY runs again, and so does npm install, which takes about thirty-eight seconds here. The timings are illustrative, but that slow reinstall is exactly the symptom.
$ docker build -t shop-api .
=> CACHED [1/4] FROM docker.io/library/node:20
=> CACHED [2/4] WORKDIR /app
=> [3/4] COPY . . 0.4s
=> [4/4] RUN npm install 38.6sLayers are checked in order
A layer is a saved snapshot of one Dockerfile step, and the cache is just those stored layers. Docker checks them top-down. FROM and WORKDIR are cached. Then COPY sees new contents in server.js, so it misses. Because of that, RUN npm install has to rebuild too.
A layer is reused only if unchanged:
Docker reuses a layer only if three things match: the instruction text, the contents of the files it uses, and the layer before it. COPY . . fails the file check. Then npm install fails the parent check, though its own line never changed.
- Its instruction text
- Its input files' contents
- The layer before it
Swap one block, restack everything above
Think of the layers as a tower of blocks. Each one rests on the block below it. When COPY . . gets swapped for a fresh block, the npm install block above it can't stay put. It has to be lifted off and built again. One miss rebuilds everything after it.
Copy dependency files first, code last
Here's the fix. We copy only package.json and package-lock.json first. The lockfile records the exact dependency versions. Then npm install runs, and it now depends only on those two files, which rarely change. The rest of the code, server.js included, gets copied last, after the install layer.
FROM node:20
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
CMD ["node", "server.js"]Same one-line edit, fixed Dockerfile
Same one-line edit to server.js, new Dockerfile. Now the log shows four CACHED steps, and that includes npm install. Only the final COPY of the source code reruns, and it takes about a third of a second. The timing is illustrative, but the saving is real.
$ docker build -t shop-api .
=> CACHED [1/5] FROM docker.io/library/node:20
=> CACHED [2/5] WORKDIR /app
=> CACHED [3/5] COPY package.json package-lock.json ./
=> CACHED [4/5] RUN npm install
=> [5/5] COPY . . 0.3sA cache miss rebuilds every layer after it; put volatile steps last.
A cache miss rebuilds every layer after it, so put the volatile steps last. Order your Dockerfile from what changes least to what changes most, and a source edit will only touch the final copy, while npm install stays cached.

