Black Friday: 50% off every workbook with code . Ends Nov 30, 23:59 ET. Buy Now

Last day: 50% off every workbook ends tonight at 23:59 ET. Code Buy Now

Skip to main content
beginner Free with email

Dockerfile Commands Cheat Sheet

Reviewed 2026-09-22

Direct answer
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, WORKDIR, COPY, ADD, RUN, ENV, ARG, LABEL, EXPOSE, VOLUME, USER, CMD, ENTRYPOINT, HEALTHCHECK, STOPSIGNAL, SHELL and ONBUILD. Most images need only FROM, WORKDIR, COPY, RUN and CMD.

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 commands by group: Build 5, Startup 4, Configuration 3, Runtime 3, Less common 2, 17 instructions in total
Docker’s 17 current Dockerfile instructions in five groups. Only ARG may come before FROM.

Dockerfile Build Instructions: FROM, WORKDIR, COPY, ADD, RUN

InstructionWhat it doesExample
FROMSets the base image and starts a build stage. Only ARG may come before itFROM node:20-alpine
WORKDIRSets the working directory for the instructions after it, creating it if neededWORKDIR /app
COPYCopies files from your build context into the imageCOPY package*.json ./
ADDLike COPY, plus remote URLs and auto-extracting local .tar archives. Prefer COPYADD app.tar.gz /app/
RUNRuns a command at build time and commits the result as a new layerRUN npm install --production
Dockerfile layer cache: copying package.json first keeps the npm install layer cached when app code changes, copying all code first reruns it
Copy package*.json before the rest of the code and an app edit reuses the cached install layer.

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

InstructionWhat it doesExample
ENVSets an environment variable that persists into the running containerENV NODE_ENV=production
ARGA build-time variable, passed with --build-arg. Gone once the build ends unless you copy it to an ENVARG NODE_VERSION=20
LABELAdds key-value metadata to the image. It doesn’t change how the image runsLABEL 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

InstructionWhat it doesExample
EXPOSEDocuments the port the container listens on. It doesn’t publish it, so you still need -p on docker runEXPOSE 3000
VOLUMEDeclares a mount point for data that should outlive the containerVOLUME /app/data
USERSets the user that runs the container and the instructions after it. Without it, the container runs as rootUSER node

USER is the instruction most beginner Dockerfiles miss, and the first one a security review flags.

Dockerfile Startup Instructions: CMD, ENTRYPOINT, HEALTHCHECK, STOPSIGNAL

InstructionWhat it doesExample
CMDThe default command, replaced by arguments to docker runCMD ["node", "server.js"]
ENTRYPOINTA fixed executable. Run arguments are appended to it (exec form)ENTRYPOINT ["node"]
HEALTHCHECKTells Docker how to test that the container works, not just runsHEALTHCHECK --interval=30s CMD curl -f http://localhost:3000/health || exit 1
STOPSIGNALThe signal Docker sends to stop the container (default SIGTERM)STOPSIGNAL SIGTERM
Dockerfile commands compared: docker run arguments replace CMD but are appended to an exec-form ENTRYPOINT, which needs --entrypoint to replace
Arguments to docker run replace CMD but are appended to an exec-form ENTRYPOINT.

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

InstructionWhat it doesExample
SHELLChanges the shell used for shell-form instructions (/bin/sh -c on Linux)SHELL ["/bin/bash", "-c"]
ONBUILDRegisters a trigger that runs when this image is used as a baseONBUILD 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.

Dockerfile multi-stage build: a node:20 build stage with the full toolchain, and a final nginx:alpine stage that copies only /app/dist
The build stage holds the toolchain. The final stage copies only the output.
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

PracticeWhy
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 instructionLimits the damage if the container is compromised
Use a .dockerignore fileKeeps node_modules, .git and secrets out of the build context
Order instructions from least to most changedMore 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

Frequently asked questions

How many instructions does a Dockerfile have?
17 current ones: ADD, ARG, CMD, COPY, ENTRYPOINT, ENV, EXPOSE, FROM, HEALTHCHECK, LABEL, ONBUILD, RUN, SHELL, STOPSIGNAL, USER, VOLUME and WORKDIR. MAINTAINER is deprecated.
How do I run commands in a Dockerfile?
Use RUN to execute a command during the build, such as installing dependencies. Use CMD or ENTRYPOINT to set what runs when the container starts. RUN happens once, at build time. CMD and ENTRYPOINT run every time the container starts.
Is a Dockerfile just a text file?
Yes. It's a plain-text file named Dockerfile with no extension. Docker looks for that exact name by default, and docker build -f points it at another.
How do I use a Dockerfile?
Write your instructions in a file named Dockerfile, then run docker build -t your-image-name . in the same directory. Docker reads it top to bottom and builds the image.
How do I create a Dockerfile?
Start with FROM for a base image, add WORKDIR and COPY to bring your code in, RUN to install what you need, and CMD to set what starts. The other 12 instructions add configuration, security and startup behaviour as you need them.
What's the difference between CMD and ENTRYPOINT?
CMD sets a default command that arguments to docker run replace. ENTRYPOINT sets a fixed executable that run arguments are appended to, and only --entrypoint replaces it.
How do I build a multi-stage Dockerfile?
Use two or more FROM lines, name the first stage with AS build, then COPY --from=build in the final stage to pull in only the output.

Questions about this PDF

No questions yet. Ask the first one.

Ask a question

Your email address won't be published. We review every question before it appears. How we handle your data. Required fields are marked

Reviewed by

CCDE/CCIE

How to cite this resource

SMEnode Labs (2026). Dockerfile Commands Cheat Sheet. SMEnode Labs. https://smenode-labs.com/free-pdf/devops/dockerfile-commands/

APA

SMEnode Labs. (2026). Dockerfile Commands Cheat Sheet. SMEnode Labs. https://smenode-labs.com/free-pdf/devops/dockerfile-commands/

BibTeX

@misc{smenodelabs2026,
  title = {Dockerfile Commands Cheat Sheet},
  author = {{SMEnode Labs}},
  year = {2026},
  url = {https://smenode-labs.com/free-pdf/devops/dockerfile-commands/},
  note = {SMEnode Labs}
}