DevOpsDocker basicsLesson 56 / 69

Write a Dockerfile

Fill-gap30 min

Write a Dockerfile

A Dockerfile is a recipe in four moves:

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "dist/main.js"]

Two habits matter more than the syntax: order for cache (dependencies copy before source code — source changes don't reinstall node_modules), and small bases (alpine). A well-ordered Dockerfile turns a 3-minute rebuild into a 3-second one.

Exercise

Write a Dockerfile for this app's backend. Justify the layer order. Then check: how does the frontend Dockerfile differ for Next.js?