Card 02/ 06

CodeDifficulty: Beginner1 min

A Dockerfile, Instruction by Instruction

Six lines, and they are most of what you will ever write.

Dockerfiletext
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]
What each instruction contributes
InstructionWhat it adds
FROMThe filesystem to start from. Every image starts from another one
WORKDIRThe directory the instructions after it run in, created if missing
COPYFiles from the build context into the image
RUNA command executed during the build, keeping whatever it changed
CMDThe command to run when a container starts. It changes nothing at build time

The highlighted lines are the three that decide the most: what you inherit, what you install, and what actually runs. RUN and CMD are the pair people confuse — one happens while the image is being made, the other every time a container starts.