# syntax=docker/dockerfile:1
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
It keeps npm's download cache between builds, so npm ci only downloads what changed.
--mount=type=cache is a BuildKit feature. BuildKit is Docker's modern build engine. The flag mounts a persistent cache folder at /root/.npm, but only while this RUN executes.
- npm stores downloaded packages in that folder.
- The folder survives between builds on the same builder.
- Its contents are not written into the image layer.
It helps exactly when the layer cache misses. Layer caching is all or nothing. If you change one dependency in package.json, npm ci runs again from scratch. With the cache mount, npm finds most packages already downloaded. It only fetches what changed, so the rebuild is much faster.
Two caveats:
- The cache lives on the builder. On a fresh CI runner it gives no benefit, unless you keep the builder or its cache between runs.
- The path must match where the tool keeps its cache. That path depends on the user the step runs as.
- It is a BuildKit cache mount that exists only during the RUN.
- The cache survives across builds on the same builder.
- The cache is not included in the final image.
- It speeds up rebuilds after a layer cache miss.
- Short-lived CI runners lose the cache unless you persist it.
Read more