When every edit triggers a dependency install, inspect the order of the Dockerfile. Copying the whole project before installing packages makes any source change invalidate the expensive layer. Copy the files used to resolve dependencies first.

dockerfile
FROM node:24-bookworm-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

This fragment assumes a project that builds into a separate output directory. Hand that output to a runtime stage suited to the application. A static site needs a file server. A server-side app may still need its production dependencies. Review the contents of the final image either way.

Check which changes break the cache

Build twice with plain progress output. Then edit a source file and build again. You should see the source and build steps rerun while the dependency step stays cached. Finally, change the lockfile and verify the install reruns.

Use a .dockerignore to keep local dependency folders, build results, Git metadata where unnecessary, and local environment files out of the build context. For private dependency credentials, use BuildKit secret mounts. A secret copied into a layer may remain in the image history after a later step deletes it.

References