Join our Discord Server

Dockerfile Reference Cheatsheet

← All cheatsheets
BuildBeginner

Dockerfile Reference Cheatsheet

Complete guide to Dockerfile instructions — FROM, RUN, COPY, ARG, ENV, ENTRYPOINT and more.


What it is

A Dockerfile is a text file with instructions to build a Docker image. Each instruction creates a layer in the image. Understanding these instructions lets you build lean, secure, and fast images.

Quick start

docker build -t myapp:latest .

Build an image from the Dockerfile in the current directory.

docker build --no-cache -t myapp:latest .

Force rebuild without layer cache.

Core instructions

InstructionExampleDescription
FROMFROM node:20-alpineSet the base image. Must be first instruction.
RUNRUN npm installExecute a command and create a new layer.
COPYCOPY . /appCopy files from build context into the image.
ADDADD app.tar.gz /appLike COPY but also extracts archives and supports URLs.
WORKDIRWORKDIR /appSet the working directory for following instructions.
ENVENV NODE_ENV=productionSet environment variable (persists at runtime).
ARGARG VERSION=1.0Build-time variable (not available at runtime).
EXPOSEEXPOSE 3000Document which port the container listens on.
CMDCMD ["node", "app.js"]Default command to run. Overrideable at runtime.
ENTRYPOINTENTRYPOINT ["node"]Defines the executable. CMD becomes default args.
USERUSER nodeSet the user for following instructions and runtime.
HEALTHCHECKHEALTHCHECK CMD curl -f http://localhost/Define a health check command.

Real-world examples

Lean Node.js Dockerfile with multi-stage build
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]

Best practices

  • Use multi-stage builds to keep production images lean.
  • Pin base image tags (e.g. node:20.11-alpine) for reproducibility.
  • Combine related RUN commands with && to minimize layers.
  • Add a .dockerignore file to exclude node_modules, .git, etc.
  • Run as a non-root USER in the final stage for security.

Related cheatsheets


Last reviewed: 2026-06-15
Join our Discord Server