Sep 3, 2026, 3:00 PM1 min read
Production Docker Engineering: Multi-Stage Builds & Container Hardening
- #Security
- #Docker
- #Containers
Containers in plain English
An image is a read-only snapshot of an application and its dependencies. A container is a running instance of that snapshot with its own isolated process, filesystem, and network view.
Multi-stage builds
Compile in a full toolchain image, ship only the runtime artifacts:
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/.next/standalone ./
USER node
EXPOSE 3000
CMD ["node", "server.js"]
Hardening checklist
- Use minimal base images (
alpine,distroless) and pin versions. - Run as a non-root
USER. - Add a
HEALTHCHECKso orchestrators can detect failures. - Keep secrets out of layers; inject them at runtime.
- Use
.dockerignoreto exclude.git,node_modules, and local env files.
Runtime flags
docker run --read-only --cap-drop ALL \
--security-opt no-new-privileges \
--pids-limit 256 --memory 512m app:latest
Layer caching
Copy dependency manifests before source code so dependency layers are reused when only application code changes. This alone often cuts build times dramatically.