
Dockerfile Order Matters: Build Cache, Layers and Multi-Stage Builds
- كاتب موثّق
محتويات المقال
RUN npm ci downloading every dependency again. On a laptop it's the same story: edit, rebuild, wait for packages that haven't changed. When someone suggests reordering the Dockerfile, the answer comes back quickly: it wouldn't help, because Docker rebuilds the whole image anyway.
The common misconception
docker build starts from an empty image and runs every line. A Dockerfile reads like a shell script, and in a script the order decides whether it works, not how long the next run takes.
What actually happens
Every instruction is a cached step
CACHED and the step costs almost nothing.
What counts as a change
COPY and ADD, the builder "calculates a cache checksum from file metadata" of the copied files, and the cache is invalidated "if the file metadata has changed for any of the files involved." The modification time alone doesn't count: in our test, running touch on the source files left every step CACHED. For RUN, the builder doesn't look at what the command would download or produce; "just the command string itself is used to find a match."
One miss invalidates everything below it
COPY . . near the top, any change anywhere in the project misses the cache at that line, and the npm ci under it runs again. The docs' advice is to "order the instructions from less frequently changed to more frequently changed where possible."
Multi-stage builds separate building from running
FROM instructions, and each one "begins a new stage of the build." You compile in one stage and copy only the output into the next, "leaving behind everything you don't want in the final image." The compiler, the type packages and the source files stay in the build stage. Each stage has its own steps in the cache, so the runtime stage's production install doesn't care when your source code changes.
The fix: dependencies first, source last
node:24.22.2-alpine3.24:
FROM node:24.22.2-alpine3.24WORKDIR /appCOPY . .RUN npm ciRUN npm run buildCMD ["node", "dist/server.js"]FROM node:24.22.2-alpine3.24 AS buildWORKDIR /appCOPY package.json package-lock.json ./RUN npm ciCOPY . .RUN npm run buildFROM node:24.22.2-alpine3.24WORKDIR /appCOPY package.json package-lock.json ./RUN npm ci --omit=devCOPY --from=build /app/dist ./distCMD ["node", "dist/server.js"]src/server.ts, and built again. The output is trimmed to the steps that matter:
docker build --progress=plain -t shop-api:wrong .# #7 [3/5] COPY . .# #7 DONE 0.2s# #8 [4/5] RUN npm ci# #8 DONE 44.1sdocker build --progress=plain -t shop-api:right .# #8 [build 4/6] RUN npm ci# #8 CACHED# #10 [build 6/6] RUN npm run build# #10 DONE 3.3s# #11 [stage-1 4/5] RUN npm ci --omit=dev# #11 CACHEDdocker image ls --format '{{.Tag}} {{.Size}}' shop-api# wrong 300MB# right 255MBCOPY . ., so the install under it ran again. Across our runs a full npm ci took between 3.8 and 44.1 seconds, depending on the network; on a real project it's whatever your full install costs. In the right version, COPY package.json package-lock.json ./ saw the same two files, so both installs came from the cache and only the TypeScript compile ran. The runtime image is also 45 MB smaller, because TypeScript and the type packages stayed behind in the build stage.
COPY compares a checksum of the files; a new modification time alone doesn't count | |
RUN apk add curl fetches fresh packages | |
How to apply it in your project
Find your first cache miss Change one line of source code and build with --progress=plain. The first step that isn'tCACHEDshows where your order breaks, and every step under it pays again.Copy dependency files before the source Copy only the files your package manager reads, such as package.jsonand the lockfile, then install, thenCOPY . .. The install now re-runs only when dependencies change.Split build and runtime stages Compile in a stage that has the SDK and dev dependencies, then start a second stage from a runtime image and copy in only the build output. Give CI a cache to reuse If your runners are ephemeral, export the cache with --cache-toand read it back with--cache-from, as Docker's docs recommend for CI/CD. Otherwise every job starts cold, whatever your order.
Key takeaways
Docker reuses each Dockerfile step from its build cache until the first step that changed; every step after it runs again. COPYmisses the cache when the copied files change, not when only their timestamps do;RUNmisses only when its text or an earlier step changes.Put what changes least at the top: copy the lockfile, install, then copy the source. A multi-stage build keeps compilers and dev dependencies out of the image you ship. A cache hit can mean stale packages, so schedule rebuilds with --pull --no-cache.
التعليقات (0)