Infrastructure / Software Engineering
Make a Node.js container reproducible enough to trust
Keep the lockfile, separate build and runtime dependencies, and test the image that will handle real traffic.

A committee hands over an event service and its Dockerfile. The next maintainer should be able to rebuild it from the recorded source revision and understand which inputs affect the output. Start with a clean install, a small final image and a smoke test against that image. Work toward more exact reproducibility by controlling the mutable inputs you uncover.
Make the dependency graph a reviewed input
Commit package-lock.json and install with npm ci. It installs the locked dependency tree and fails if the manifest and lockfile disagree. If your project requires flags that change the dependency tree, record those non-secret settings in a project-level configuration so local and build environments agree. Keep the Node major version and package-manager version explicit. npm ci reference
Copy the manifests into the image first so a source-only change can reuse the dependency layer. The cache saves time. A clean build should still work when it is empty. Exclude node_modules, local build output, the Git directory and local environment files in .dockerignore. Do not bake access tokens into the image. If an install genuinely needs a private registry credential, supply it through a build secret. Docker build secrets
Build in one stage and run in another
This example assumes a single npm package with a build script that writes dist/server.js, and a server configured to listen on 0.0.0.0:3000. It uses a supported Node release line. Keep the same base family for development and production dependencies when native packages are involved.
# syntax=docker/dockerfile:1
ARG NODE_IMAGE=node:22-bookworm-slim
FROM ${NODE_IMAGE} AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
FROM ${NODE_IMAGE} AS dependencies
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
FROM ${NODE_IMAGE} AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
Multi-stage builds let you copy only the server artifacts and dependencies the runtime needs. The final stage runs as an unprivileged user. Application files remain root-owned and readable in this example. Put writable application data in an explicitly writable, separately provisioned path if your service needs it. Docker multi-stage builds
Pin the inputs that can still move
The tag in the example makes it easy to try. Tags can point to different image content later. In a release pipeline, resolve the reviewed base to a digest and pass that exact reference as NODE_IMAGE. Update it deliberately so security fixes still arrive. Record the target platform because a build for ARM64 can legitimately produce different native artifacts from one for AMD64.
A lockfile does not control an install script that downloads a dependency at build time, and a digest does not control a build step that embeds the current timestamp. Identify those inputs before claiming that two container builds will be byte-identical. Docker guidance on pinning base images
Test the exact artifact you publish
Build the runtime image and run its real entry point. Bind a test instance to localhost, request a health endpoint and perform one representative read. Exercise whatever configuration your deployment passes without putting production secrets into the test. Promote the tested image by its registry digest so deployment uses the same artifact.
Cover illustration by ACM MPSTME.