Build small, build reproducible
A multi-stage build keeps compilers and package managers out of the runtime image. Smaller images pull faster, expose less, and are cheaper to scan.
FROM node:22-bookworm-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
RUN npm run build
FROM gcr.io/distroless/nodejs22-debian12
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER 10001
EXPOSE 8080
CMD ["dist/server.js"]Runtime hardening defaults
Run as a non-root UID and set read_only: true with explicit tmpfs mounts.
Drop all capabilities and add back only what the process needs.
Set memory and CPU limits so one container cannot starve the host.
Pin images by digest in production, not by a floating tag.
services:
api:
image: registry.internal/api@sha256:9f2c...
read_only: true
user: "10001:10001"
cap_drop: ["ALL"]
security_opt: ["no-new-privileges:true"]
tmpfs: ["/tmp"]
deploy:
resources:
limits: { cpus: "1.0", memory: 512M }
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8080/healthz"]
interval: 15s
timeout: 3s
retries: 3
logging:
driver: json-file
options: { max-size: "10m", max-file: "3" }Secrets do not belong in the image
Build arguments end up in the image history. Use runtime secrets — Docker secrets, a mounted file from a secret manager, or environment injection at deploy time — and rotate them independently of releases.
Health, restart and drain behaviour
A healthcheck that only verifies the process is alive is useless. Probe a path that touches the dependencies you care about, and make the application handle SIGTERM by draining connections before exit.
Keep an upgrade path
docker compose pull
docker compose up -d --no-deps --wait api
docker image prune -f --filter "until=168h"