CodePark/ questions / Layers & build cache
START PRACTICINGPRACTICE
Backend question · Docker › Layers & build cache

You edit one .ts file and rebuild. Which version reuses the cached npm ci layer?

# Version A
FROM node:22-slim
WORKDIR /app
COPY . .
RUN npm ci

# Version B
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
Version B

"Version B" reuses the cache. In Version B, npm ci depends only on package.json and package-lock.json. Those files did not change, so the cache still matches. Only the final COPY runs again.

"Version A" does not reuse it. In Version A,
COPY . . copies the edited file. This invalidates that layer. The npm ci step after it is then rebuilt too.