InterviewHubPRO
Section Overview
Q51

Question 51 of 60

Part 5: Production Scale, Deployment & Observability (51 - 60)

85%
Node.js & Libuv•Part 5: Production Scale, Deployment & Observability (51 - 60)

Containerized Docker Best Practices for Node.js.

In Simple Words:

  1. Use multi-stage builds.
  2. Run on slim Alpine or distroless images.
  3. Run as non-root user (USER node).
  4. Use tini as init process (ENTRYPOINT ["tini", "--"]) to properly reap zombie child processes and handle signal forwarding (PID 1 problem).
dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-alpine
RUN apk add --no-cache tini
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/dist ./dist
USER node
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "dist/server.js"]