ToolSnippet
🐳Developer Cheat Sheet

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.

1-Click Syntax CopyProduction-Tested StandardsInteractive Tool Companion

Need to generate production-ready Dockerfiles?

Build hardened multi-stage Dockerfiles for Node.js, Python, Go, Rust, and Nginx with non-root security.

Launch Dockerfile & Multi-Stage Builder

Core Dockerfile Instructions

Essential build directives used across all container images.

Syntax / TokenName / TypeDescriptionExample / Notes
FROM <image>[:tag] [AS <name>]
Base ImageInitializes a new build stage and sets base imageFROM node:20-alpine AS builder
WORKDIR /path/to/dir
Working DirectorySets working directory for subsequent RUN, CMD, ENTRYPOINT, COPYWORKDIR /app
COPY [--from=<name>] <src> <dest>
Copy FilesCopies new files or directories from host into container filesystemCOPY --from=builder /app/dist ./dist
ADD <src> <dest>
Add (with Tar extraction/URLs)Like COPY, but automatically unpacks local tar archives and fetches URLsADD package.tar.gz /app/
RUN <command>
Execute Build CommandExecutes commands in a new layer on top of current image and commits resultRUN npm ci --only=production
ENV <key>=<value>
Environment VariableSets persistent environment variables available at build and runtimeENV NODE_ENV=production
ARG <name>[=<default>]
Build-Time ArgumentDefines variables that users can pass at build-time with --build-argARG VERSION=1.0.0
EXPOSE <port>[/<protocol>]
Expose PortDocuments network ports that container listens on at runtimeEXPOSE 3000
USER <user>[:<group>]
Run as Non-Root UserSets user name or UID to use when running image and for RUN/CMD instructionsUSER node

Execution: CMD vs ENTRYPOINT

Configuring how containers start and handle CLI arguments.

Syntax / TokenName / TypeDescriptionExample / Notes
CMD ["executable", "param1", "param2"]
Default Command (Exec form)Provides default command or arguments that can be easily overridden by docker runCMD ["node", "server.js"]
ENTRYPOINT ["executable", "param1"]
Fixed Entrypoint (Exec form)Configures container to run as an executable; parameters from CMD or CLI append to itENTRYPOINT ["npm", "start"]
HEALTHCHECK [options] CMD <command>
HealthcheckTells Docker how to test container to check that it is still workingHEALTHCHECK CMD curl -f http://localhost:3000/api/health || exit 1
VOLUME ["/path/to/dir"]
Mount VolumeCreates a mount point with specified path and marks it as holding externally mounted volumeVOLUME ["/data"]

Frequently Asked Questions

Common questions and developer tips regarding dockerfile instructions & directives reference.

Explore More Cheat Sheets

All Cheat Sheets →