Dockerfile Instructions Cheat Sheet: Every Keyword Explained
Every Dockerfile instruction with the gotchas that trip people up.
Build instructions, runtime config, and multi-stage patterns.
Instructions
| Instruction | Purpose |
|---|---|
| FROM img AS stage | Base image / named stage |
| RUN cmd | Execute at build time (new layer) |
| COPY src dst | Copy files (preferred over ADD) |
| ADD url/tar dst | COPY + auto-extract / fetch |
| WORKDIR /app | Set working dir |
| ENV K=V | Persistent env var |
| ARG K | Build-time variable |
| EXPOSE 8080 | Document port (no publish) |
| USER node | Drop root |
| VOLUME /data | Declare mount point |
| HEALTHCHECK | Container liveness probe |
ENTRYPOINT vs CMD
| Form | Behavior |
|---|---|
| ENTRYPOINT ["app"] | Fixed executable |
| CMD ["--flag"] | Default args (overridable) |
| CMD ["app", "--flag"] | Default command, fully overridable |
Multi-stage build
Dockerfile
FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/htmlKey takeaways
- Prefer COPY over ADD unless you need auto-extract or remote fetch.
- ENTRYPOINT sets the fixed binary; CMD supplies overridable defaults.
- Order layers least-to-most volatile to maximize cache hits.
Related guides
Docker Cheat Sheet: Commands, Dockerfile & ComposeA Docker cheat sheet - build, run, images, volumes, networks, Dockerfile instructions, and compose commands f…
Docker CLI Cheat Sheet: Every Command You Use DailyA Docker CLI cheat sheet - run, build, ps, exec, logs, images, volumes, networks, and the prune commands for…
Docker Compose Cheat Sheet: Commands & compose.yaml KeysA docker compose cheat sheet - up, down, build, logs, exec commands plus the compose.yaml service keys you co…