Dockerfile Cheat Sheet

High-value Dockerfile instructions, patterns, and build flags for reproducible and efficient image builds.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Dockerfile Basics
Build from Dockerfile
docker build -t myapp:dev .

# Build an image from the Dockerfile in the current directory.

Build with alternate Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .

# Build using an alternate Dockerfile path.

Use FROM instruction
FROM node:20-alpine

# Set the base image for a Dockerfile stage.

Use WORKDIR instruction
WORKDIR /app

# Set the working directory for subsequent instructions.

Copy files into image
COPY package*.json ./

# Copy files from the build context into the image.

Run build step
RUN npm ci

# Execute a command during image build.

Set default command
CMD ["node", "server.js"]

# Set the default command when the container starts.

Set entrypoint
ENTRYPOINT ["/usr/local/bin/app"]

# Set the container entrypoint executable.

Document a listening port
EXPOSE 3000

# Document the port the application listens on.

Set environment variable
ENV NODE_ENV=production

# Set environment variables in the image.

Define build argument
ARG NODE_VERSION=20

# Define a build-time variable.

Set image metadata label
LABEL org.opencontainers.image.source="https://github.com/acme/myapp"

# Attach metadata labels to the image.

## Common Patterns
Use multi-stage build
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html

# Build artifacts in one stage and copy only runtime output into the final stage.

Run as non-root
RUN addgroup -S app && adduser -S app -G app
USER app

# Switch the runtime user away from root.

Add health check
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD wget -qO- http://localhost:3000/health || exit 1

# Define a health check command for the container.

Copy with ownership
COPY --chown=node:node . /app

# Copy files with explicit ownership.

Cache dependencies effectively
COPY package*.json ./
RUN npm ci
COPY . .

# Copy dependency manifests before application code to maximize layer cache reuse.

Use .dockerignore
node_modules
dist
.git
.env
coverage

# Exclude unnecessary files from build context.

## Build Flags and Targets
Build a specific stage
docker build --target build -t myapp:build .

# Build only up to a named stage.

Pass build args
docker build --build-arg NODE_VERSION=20 -t myapp:dev .

# Pass values for Dockerfile `ARG` instructions.

Build without cache
docker build --no-cache -t myapp:clean .

# Rebuild all steps without using cache.

Always pull newer base image
docker build --pull -t myapp:dev .

# Attempt to pull a newer base image before build.

Build for specific platform
docker build --platform linux/amd64 -t myapp:amd64 .

# Build for a target platform when supported.

Use plain progress output
docker build --progress=plain -t myapp:dev .

# Render verbose plain-text build output.