A Dockerfile is a plain-text file of instructions that docker build runs top to bottom to build an image. Docker has 17 current Dockerfile instructions: FROM (base image), WORKDIR, COPY, ADD, RUN (build-time command), ENV, ARG, LABEL, EXPOSE, VOLUME, USER, CMD (default start command), ENTRYPOINT, HEALTHCHECK, STOPSIGNAL, SHELL and ONBUILD. Most images need only FROM, WORKDIR, COPY, RUN and CMD. This Dockerfile commands cheat sheet explains all 17 with an example each. The free PDF is a Docker guide that covers the core nine. Drop your email and it lands in your inbox.
Dockerfile Commands Cheat Sheet: A Working Example, Line by Line
Here’s a working Dockerfile for a Node.js app. Every instruction in it is explained in the tables below.
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Looking for docker run and docker build instead? See our Docker Commands Cheat Sheet. For multi-container apps, see the Docker Compose commands cheat sheet.
Running the image on Kubernetes? See the Kubernetes troubleshooting cheat sheet.

Dockerfile Build Instructions: FROM, WORKDIR, COPY, ADD, RUN
| Instruction | What it does | Example |
|---|---|---|
| FROM | Sets the base image and starts a build stage. Only ARG may come before it | FROM node:20-alpine |
| WORKDIR | Sets the working directory for the instructions after it, creating it if needed | WORKDIR /app |
| COPY | Copies files from your build context into the image | COPY package*.json ./ |
| ADD | Like COPY, plus remote URLs and auto-extracting local .tar archives. Prefer COPY | ADD app.tar.gz /app/ |
| RUN | Runs a command at build time and commits the result as a new layer | RUN npm install --production |

Why does COPY package*.json ./ come before COPY . .? Per Docker’s build cache docs, once a layer changes, every layer after it is rebuilt. Copy only the dependency manifest, install, then copy the rest. Editing code then reruns only the fast COPY . . step, not the install.
Dockerfile Configuration Instructions: ENV, ARG, LABEL
| Instruction | What it does | Example |
|---|---|---|
| ENV | Sets an environment variable that persists into the running container | ENV NODE_ENV=production |
| ARG | A build-time variable, passed with --build-arg. Gone once the build ends unless you copy it to an ENV | ARG NODE_VERSION=20 |
| LABEL | Adds key-value metadata to the image. It doesn’t change how the image runs | LABEL version="1.0" |
If a script in the running container needs a build-time value, assign the ARG to an ENV in the same Dockerfile.
Dockerfile Runtime Instructions: EXPOSE, VOLUME, USER
| Instruction | What it does | Example |
|---|---|---|
| EXPOSE | Documents the port the container listens on. It doesn’t publish it, so you still need -p on docker run | EXPOSE 3000 |
| VOLUME | Declares a mount point for data that should outlive the container | VOLUME /app/data |
| USER | Sets the user that runs the container and the instructions after it. Without it, the container runs as root | USER node |
USER is the instruction most beginner Dockerfiles miss, and the first one a security review flags.
Dockerfile Startup Instructions: CMD, ENTRYPOINT, HEALTHCHECK, STOPSIGNAL
| Instruction | What it does | Example |
|---|---|---|
| CMD | The default command, replaced by arguments to docker run | CMD ["node", "server.js"] |
| ENTRYPOINT | A fixed executable. Run arguments are appended to it (exec form) | ENTRYPOINT ["node"] |
| HEALTHCHECK | Tells Docker how to test that the container works, not just runs | HEALTHCHECK --interval=30s CMD curl -f http://localhost:3000/health || exit 1 |
| STOPSIGNAL | The signal Docker sends to stop the container (default SIGTERM) | STOPSIGNAL SIGTERM |

Per Docker’s Dockerfile reference, docker run image args replaces CMD but appends to an exec-form ENTRYPOINT. Only --entrypoint replaces the ENTRYPOINT, and a shell-form ENTRYPOINT ignores run arguments. Use CMD alone for an easy-to-override default. Use ENTRYPOINT plus CMD when the executable is fixed and the arguments aren’t. The HEALTHCHECK example needs curl inside the image.
Less Common Dockerfile Instructions: SHELL and ONBUILD
| Instruction | What it does | Example |
|---|---|---|
| SHELL | Changes the shell used for shell-form instructions (/bin/sh -c on Linux) | SHELL ["/bin/bash", "-c"] |
| ONBUILD | Registers a trigger that runs when this image is used as a base | ONBUILD COPY . /app/src |
MAINTAINER also still exists, but Docker marks it deprecated. Use LABEL instead.
Dockerfile Multi-Stage Build: One Dockerfile, Two Stages
A single-stage Dockerfile ships your build tools inside the final image. A multi-stage build uses several FROM lines: one stage builds, the last copies only the output, leaving a small production image, as Docker’s multi-stage guide describes.

FROM node:20 AS build
WORKDIR /app
COPY . .
RUN npm install && npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
The final image is nginx:alpine plus your compiled files: no Node.js, no node_modules.
Dockerfile Best Practices
| Practice | Why |
|---|---|
Use a small base image (node:20-alpine) | Smaller pulls, less attack surface |
Pin version tags (node:20.11-alpine, not latest) | latest can change under you |
Add a USER instruction | Limits the damage if the container is compromised |
Use a .dockerignore file | Keeps node_modules, .git and secrets out of the build context |
| Order instructions from least to most changed | More cache reuse, faster rebuilds |
What’s Inside the Dockerfile Commands Cheat Sheet PDF
The PDF is a 14-page Docker guide, and the Dockerfile material is one section of it.
- Dockerfile section: the Node.js example above, plus FROM, WORKDIR, COPY, ADD, RUN, ENV, EXPOSE, CMD and ENTRYPOINT, and the multi-stage example
- Also in the PDF: images, containers, volumes, networking, Docker Compose, an end-to-end scenario and a quick-reference page
- Not in the PDF: ARG, LABEL, VOLUME, USER, HEALTHCHECK, STOPSIGNAL, SHELL and ONBUILD. They’re on this page.
Who It’s For
- Beginners writing their first Dockerfile, no experience assumed
- Developers who ship containers but forget the HEALTHCHECK syntax or when ARG beats ENV
- Anyone studying for a DevOps interview or certification
No questions yet. Ask the first one.