Dockerfile Instructions
What each instruction means, with copy-exact examples. Following the official Dockerfile reference (as of 2026-06).
Instruction overview
| Instruction | Purpose |
|---|---|
FROM | Specify the base image; must be the first effective instruction. AS <name> names a stage |
RUN | Run a command at build time (install, compile). Each RUN is a layer |
CMD | Default command at container start; overridable by what you append to docker run |
ENTRYPOINT | Fixed entry point at container start; docker run args do not override it |
COPY | Copy files from the context into the image (the common choice) |
ADD | Like COPY but also auto-extracts tar and downloads URLs (prefer COPY) |
ENV | Set an env var; stays in the image, visible at runtime |
ARG | Build-time variable via --build-arg; does not stay in the image |
WORKDIR | Set the working dir (auto-created), replacing RUN cd |
EXPOSE | Documentary port declaration; does not actually open a port |
VOLUME | Mark a path as an external volume mount point |
USER | Set the identity for following instructions and the container (use non-root) |
LABEL | Add metadata (replaces the deprecated MAINTAINER) |
HEALTHCHECK | Define a health-check command |
ONBUILD | Deferred trigger: runs when this image is used as a base |
SHELL | Override the shell used by shell form |
STOPSIGNAL | Signal sent on docker stop (default SIGTERM) |
RUN shell form vs exec form
RUN into one (with && and \) reduces layers and cleans the apt cache in the same layer.
The three commonly confused pairs
- CMD vs ENTRYPOINT
- COPY vs ADD
- ARG vs ENV
CMDis the “default command”;docker run myimg other-commandcompletely overrides CMD.ENTRYPOINTis the “fixed entry point”; args appended todocker runare appended after ENTRYPOINT, not overriding it.- Common combo:
ENTRYPOINTholds the fixed executable,CMDholds overridable default args. - Write both in exec form (JSON array) so PID 1 is the app itself and can receive SIGTERM for a graceful shutdown; shell form wraps a
/bin/sh -cand the signal never reaches the app.
| No ENTRYPOINT | ENTRYPOINT ["ep"] (exec) | ENTRYPOINT ep (shell) | |
|---|---|---|---|
| No CMD | error | ep | /bin/sh -c ep |
CMD ["cmd"] | cmd | ep cmd | /bin/sh -c ep (CMD ignored) |
CMD cmd | /bin/sh -c cmd | ep /bin/sh -c cmd | /bin/sh -c ep (CMD ignored) |
Other common instruction examples
EXPOSE is only a declaration, it does not open a port; a non-existent user for USER must be created first with RUN useradd.
Next
- A Python app Dockerfile: assembling these instructions into a working Dockerfile.
- Layer cache and best practices: instruction order and caching.