SkillByAIOpen interactive version →

Lesson 24 / 32

Docker & Containerization

Containerize a Fastify app with a minimal Dockerfile and a multi-stage build for smaller images.

Dockerfile for Fastify

A minimal Dockerfile to run your Fastify app in a container.

FROM node:22-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --omit=dev

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

Multi-stage builds

Reduce image size by building in one stage and copying the essentials to a final stage.

FROM node:22 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .

FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app . 
EXPOSE 3000
CMD ["node", "server.js"]