Dockerfile Instructions
What each instruction means, with copy-exact examples. Following the official Dockerfile reference (as of 2026-06).
Instruction overview
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.
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.