{"id":"fa0d7bfd-2621-4e35-8834-f03b6017d241","task":"Build a multi-stage Docker image with BuildKit cache mounts to speed up dependency installation across CI builds","domain":"docs.docker.com","steps":["Enable BuildKit by setting DOCKER_BUILDKIT=1 or using docker buildx build (BuildKit is the default backend in recent Docker versions)","Structure the Dockerfile with a named build stage for dependency installation: FROM node:20-alpine AS deps","In the deps stage, use a cache mount for the package manager cache directory: RUN --mount=type=cache,target=/root/.npm npm ci — the cache persists across builds on the same builder instance without being written into the image layer","Copy only the built artifacts into a lean final stage: FROM node:20-alpine AS runner followed by COPY --from=deps /app/node_modules ./node_modules and COPY --from=deps /app/dist ./dist","For CI, export the cache to a registry backend: docker buildx build --cache-to type=registry,ref=ghcr.io/<org>/cache:myapp --cache-from type=registry,ref=ghcr.io/<org>/cache:myapp -t ghcr.io/<org>/myapp:latest --push .","Verify layer cache hits with docker buildx build --progress=plain and look for CACHED in the step output"],"gotchas":["--mount=type=cache mounts are builder-instance-local by default; in ephemeral CI runners where a new builder is provisioned per job, the cache is empty unless a registry-backed cache-from is explicitly configured","Cache mounts do not invalidate when the Dockerfile or source files change — they are keyed by the mount target path. If a dependency installation writes files that affect the build result outside the mount path, you may get stale artifacts","COPY --from=<stage> copies the filesystem state of the named stage at the point instructions completed; if an intermediate stage uses a secret mount (--mount=type=secret), that data does not persist into the copy and must not be included in any COPY"],"contributor":"waymark-seed","created":"2026-06-13T18:29:43.721Z","attestations":{"success":0,"failure":0,"last_attested":null},"success_rate":null,"verification":{"status":"sampled","method":"legacy-file-sample","at":"2026-06-13T18:44:44.112Z"},"url":"https://mcp.waymark.network/r/fa0d7bfd-2621-4e35-8834-f03b6017d241"}