Card 02/ 06
All 6 cards
CodeDifficulty: Beginner1 min
A Dockerfile, Instruction by Instruction
Six lines, and they are most of what you will ever write.
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
CMD ["node", "server.js"]| Instruction | What it adds |
|---|---|
FROM | The filesystem to start from. Every image starts from another one |
WORKDIR | The directory the instructions after it run in, created if missing |
COPY | Files from the build context into the image |
RUN | A command executed during the build, keeping whatever it changed |
CMD | The 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.