Dockerfile Instructions, Directives & Multi-Stage Reference
Complete syntax guide for Dockerfile instructions (FROM, RUN, CMD, ENTRYPOINT, COPY, ENV, ARG, HEALTHCHECK) with multi-stage build best practices.
Need to generate production-ready Dockerfiles?
Build hardened multi-stage Dockerfiles for Node.js, Python, Go, Rust, and Nginx with non-root security.
Core Dockerfile Instructions
Essential build directives used across all container images.
| Syntax / Token | Name / Type | Description | Example / Notes |
|---|---|---|---|
FROM <image>[:tag] [AS <name>] | Base Image | Initializes a new build stage and sets base image | FROM node:20-alpine AS builder |
WORKDIR /path/to/dir | Working Directory | Sets working directory for subsequent RUN, CMD, ENTRYPOINT, COPY | WORKDIR /app |
COPY [--from=<name>] <src> <dest> | Copy Files | Copies new files or directories from host into container filesystem | COPY --from=builder /app/dist ./dist |
ADD <src> <dest> | Add (with Tar extraction/URLs) | Like COPY, but automatically unpacks local tar archives and fetches URLs | ADD package.tar.gz /app/ |
RUN <command> | Execute Build Command | Executes commands in a new layer on top of current image and commits result | RUN npm ci --only=production |
ENV <key>=<value> | Environment Variable | Sets persistent environment variables available at build and runtime | ENV NODE_ENV=production |
ARG <name>[=<default>] | Build-Time Argument | Defines variables that users can pass at build-time with --build-arg | ARG VERSION=1.0.0 |
EXPOSE <port>[/<protocol>] | Expose Port | Documents network ports that container listens on at runtime | EXPOSE 3000 |
USER <user>[:<group>] | Run as Non-Root User | Sets user name or UID to use when running image and for RUN/CMD instructions | USER node |
Execution: CMD vs ENTRYPOINT
Configuring how containers start and handle CLI arguments.
| Syntax / Token | Name / Type | Description | Example / Notes |
|---|---|---|---|
CMD ["executable", "param1", "param2"] | Default Command (Exec form) | Provides default command or arguments that can be easily overridden by docker run | CMD ["node", "server.js"] |
ENTRYPOINT ["executable", "param1"] | Fixed Entrypoint (Exec form) | Configures container to run as an executable; parameters from CMD or CLI append to it | ENTRYPOINT ["npm", "start"] |
HEALTHCHECK [options] CMD <command> | Healthcheck | Tells Docker how to test container to check that it is still working | HEALTHCHECK CMD curl -f http://localhost:3000/api/health || exit 1 |
VOLUME ["/path/to/dir"] | Mount Volume | Creates a mount point with specified path and marks it as holding externally mounted volume | VOLUME ["/data"] |
Frequently Asked Questions
Common questions and developer tips regarding dockerfile instructions & directives reference.
Explore More Cheat Sheets
All Cheat Sheets →Regular Expression (RegEx) Syntax & Tokens
A complete developer cheat sheet for RegEx tokens, character classes, anchors, lookaheads, lookbehinds, flags, and common production patterns with 1-click copy.
Crontab Syntax & Schedule Reference
Quick reference guide for Unix/Linux crontab schedule syntax, 5-field time breakdown, step values, special characters, and common cron presets.
HTTP Status Codes (1xx–5xx) Quick Reference
Comprehensive quick reference for all standard RFC HTTP status codes, REST API conventions, caching behaviors, and client/server error definitions.