November 18, 2026 路 #63
DockerfileEasyOpen write-up
Every commit reinstalls everything
A one-line code change makes the build reinstall every npm package and take six minutes. Which line breaks the cache?
FROM node:22-slim
WORKDIR /app
COPY . .
RUN npm ci --omit=dev
USER node
CMD ["node", "server.js"]Next puzzle in UTC
One try. The first answer sticks.
How to fix it
A layer is reused only if its inputs are unchanged. Copying all files first makes every commit change the input to npm ci. Copy package.json and the lockfile first.
Docker hashes the files a COPY brings in. When COPY . . includes your source, any edit changes that layer and every layer after it, including npm ci. Dependencies change rarely, so they should be installed from a layer that depends only on the manifest files.
Fix
FROM node:22-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
COPY . .
USER node
CMD ["node", "server.js"]
Add a .dockerignore with node_modules, .git, and .env so the context is small and secrets stay out of the image.