Back to devops
devops#docker#node#devops

The Perfect Dockerfile for Node.js Apps

Multi-stage builds, layer caching and a 90% smaller image size.

Jane Contributor August 2, 2026 3 views

The Perfect Dockerfile for Node.js

# Stage 1 — deps
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production

# Stage 2 — build
FROM node:20-alpine AS build
WORKDIR /app
COPY . .
RUN yarn install --frozen-lockfile && yarn build

# Stage 3 — runtime
FROM node:20-alpine
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]

Result: ~120MB vs 900MB for a naive Dockerfile.

Keep reading

You may also like

Discussion (0)

No comments yet. Be the first to weigh in.

Leave a comment

Comments are reviewed before appearing.