Lesson 22 / 30

Docker & Containerization

Containerize your Fastify application with Docker.

Dockerfile for Fastify

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

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm ci --only=production

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:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .

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