Complete Guide to Docker in Production
Run Docker in production: multi-stage builds, distroless images, health checks, scanning, resource limits, secrets, and runtime security.
Introduction
Docker is the standard for packaging and deploying applications. But a container that works on your laptop and a container that survives production are different things: production images need to be small, fast to start, non-root, health-checked, scanned for vulnerabilities, capped on resources, and stripped of secrets baked into layers.
This guide covers the full production surface: multi-stage builds and layer caching, distroless images, health checks, image scanning in CI, resource limits, logging drivers, secret handling, multi-architecture builds, and runtime hardening. For the wider pipeline around the container (signing, provenance, SBOMs) see the supply chain security guide.
Multi-Stage Builds
Multi-stage builds reduce image size by separating the build environment from the runtime environment. Only the final stage’s artifacts are included in the image — compilers, package managers, and test dependencies never reach production.
The other half of fast, small builds is .dockerignore — without it, COPY . . drags .git, node_modules, and test data into every layer:
# .dockerignore
.git
.gitignore
node_modules
**/__pycache__
**/*.pyc
**/*.test.js
tests/
docs/
*.md
.env*
coverage/
dist/
.vscode/
.idea/
Order your Dockerfile by change frequency: OS packages, then dependency manifests, then npm ci/pip install, and only then your source. Layers before a changed line rebuild — putting COPY . . last keeps dependency layers cached across most commits.
Python Multi-Stage Build
# Stage 1: Builder
FROM python:3.12-slim AS builder
WORKDIR /build
# Install build dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt
# Copy application code
COPY . .
# Build any artifacts (e.g., compile translations, bundle assets)
RUN python -m compileall .
# Stage 2: Runtime (distroless)
FROM gcr.io/distroless/python3-debian12:nonroot
WORKDIR /app
# Copy installed packages from builder
COPY --from=builder /root/.local/lib/python3.12/site-packages /usr/lib/python3.12/site-packages
COPY --from=builder /build /app
ENV PYTHONPATH=/usr/lib/python3.12/site-packages
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
EXPOSE 8000
CMD ["-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Node.js Multi-Stage Build
# Stage 1: Install dependencies
FROM node:20-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --only=production
# Stage 2: Build
FROM node:20-slim AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 3: Runtime
FROM node:20-slim AS runtime
WORKDIR /app
# Copy production dependencies
COPY --from=deps /app/node_modules ./node_modules
# Copy built artifacts
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./
# Run as non-root
RUN groupadd -r appuser && useradd -r -g appuser appuser
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "fetch('http://localhost:3000/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
CMD ["node", "dist/server.js"]
Java Multi-Stage Build
# Stage 1: Build
FROM maven:3.9-eclipse-temurin-21 AS builder
WORKDIR /build
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package -DskipTests
# Stage 2: Runtime
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
# Copy JAR from builder
COPY --from=builder /build/target/*.jar app.jar
# Run as non-root
RUN addgroup -S appuser && adduser -S appuser -G appuser
USER appuser
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD wget -q --spider http://localhost:8080/actuator/health || exit 1
CMD ["java", "-XX:MaxRAMPercentage=75.0", "-jar", "app.jar"]
Distroless Images
Distroless images contain only your application and its runtime dependencies. No shell, no package manager, no utilities. This reduces attack surface and image size.
# Google distroless images
FROM gcr.io/distroless/python3-debian12 # Python
FROM gcr.io/distroless/node20-debian12 # Node.js
FROM gcr.io/distroless/java21-debian12 # Java
FROM gcr.io/distroless/static-debian12 # Go, Rust (static binaries)
# Distroless with debug shell (for troubleshooting only)
FROM gcr.io/distroless/python3-debian12:debug
# Usage with static binary (Go)
FROM gcr.io/distroless/static-debian12:nonroot
COPY myapp /myapp
USER nonroot:nonroot
CMD ["/myapp"]
Distroless vs Alpine vs Slim
Image sizes (Python 3.12):
python:3.12 ~1GB Full Debian — never use in production
python:3.12-slim ~150MB Debian without docs/headers — good default
python:3.12-alpine ~50MB musl libc — smaller but may break packages
distroless python3 ~50MB No shell, no package manager — most secure
Trade-offs:
Distroless: Smallest, most secure, but no shell for debugging
Slim: Good balance, shell available for debugging
Alpine: Smallest with shell, but musl libc can cause issues
Full: Never use in production — too large, too many tools
The debugging trade-off is real: without a shell you can’t docker exec -it bash into the container. Keep the :debug tag variant available for incidents, or debug through ephemeral containers and structured logs instead. Whatever you choose, pin images by digest (python:3.12-slim@sha256:…) so a tag update can’t silently change what you deploy.
Health Checks
A health check tells the orchestrator whether the container is alive and whether it’s ready to take traffic — two different questions.
# HTTP health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# TCP health check (no curl available)
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD nc -z localhost 8000 || exit 1
# Process health check (distroless — no curl/nc)
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD ["/app/healthcheck"]
Two endpoints, two jobs. /health answers “is the process alive?” — keep it free of dependencies so a dead database doesn’t trigger restart loops. /health/ready answers “should this instance take traffic?” — that’s where dependency checks belong:
# Application health endpoint
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/health")
async def health():
"""Liveness probe — is the process running?"""
return {"status": "healthy"}
@app.get("/health/ready")
async def readiness():
"""Readiness probe — can we serve requests?"""
checks = {
"database": await check_database(),
"redis": await check_redis(),
"external_api": await check_external_api()
}
all_healthy = all(checks.values())
if not all_healthy:
return JSONResponse(
status_code=503,
content={"status": "not ready", "checks": checks}
)
return {"status": "ready", "checks": checks}
async def check_database() -> bool:
try:
db.execute("SELECT 1")
return True
except:
return False
async def check_redis() -> bool:
try:
redis.ping()
return True
except:
return False
A caveat worth knowing: docker run and Docker Compose honor HEALTHCHECK, but Kubernetes ignores it — it uses its own liveness/readiness probes in the pod spec. Define health in both places if you run on Kubernetes.
Image Scanning
Build the image, then scan it before it reaches a registry. Trivy and Grype are the go-to open-source scanners; Docker Scout ships with Docker Desktop; Snyk covers the commercial end.
# Trivy — open source scanner
trivy image myapp:latest
trivy image --severity HIGH,CRITICAL myapp:latest
trivy image --ignore-unfixed myapp:latest
# Grype — another open source scanner
grype myapp:latest
# Docker Scout (Docker's built-in scanner)
docker scout cves myapp:latest
# Snyk — commercial scanner
snyk container test myapp:latest
# CI/CD: Scan images in pipeline
name: Container Security
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build -t myapp:${{ github.sha }} .
- name: Trivy scan
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
severity: HIGH,CRITICAL
exit-code: 1 # Fail build on critical vulns
format: json
output: trivy-report.json
- name: Upload report
uses: actions/upload-artifact@v4
with:
name: trivy-report
path: trivy-report.json
--severity HIGH,CRITICAL with exit-code: 1 is what turns a scan into a gate. --ignore-unfixed filters noise: vulnerabilities without a published fix can’t be remediated anyway — hiding them keeps the gate meaningful. Most findings land in the base image; a smaller base (slim, distroless) shrinks your CVE surface before you scan a single line of app code.
Resource Limits
Containers without limits can starve the host — and in shared environments, one leaking process takes down everyone else. limits cap what the container may use; reservations guarantee what it will get when the host contends.
# docker-compose.yml with resource limits
version: "3.9"
services:
api:
image: myapp:latest
deploy:
resources:
limits:
cpus: "2.0"
memory: 1G
reservations:
cpus: "0.5"
memory: 256M
ulimits:
nofile:
soft: 65536
hard: 65536
nproc: 4096
pids_limit: 4096
worker:
image: myapp-worker:latest
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
# Docker run with resource limits
docker run -d \
--name api \
--cpus="2.0" \
--memory="1g" \
--memory-swap="1g" \
--memory-reservation="256m" \
--pids-limit="4096" \
--ulimit nofile=65536:65536 \
--restart unless-stopped \
myapp:latest
When a container exceeds its memory limit it gets OOM-killed — docker events and dmesg show oom_kill entries, and docker inspect reports OOMKilled: true. Watch for two pitfalls: --memory-swap equal to --memory disables swap (usually what you want in containers), and JVM apps need -XX:MaxRAMPercentage so the heap respects the cgroup limit instead of the host’s RAM.
Logging
Log to stdout and stderr — never to files inside the container. The Docker logging driver collects the streams; a JSON line per event makes downstream parsing trivial.
# Structured JSON logging for containers
import logging
import json
import sys
from datetime import datetime
class JSONFormatter(logging.Formatter):
def format(self, record):
log_entry = {
"timestamp": datetime.utcnow().isoformat() + "Z",
"level": record.levelname.lower(),
"message": record.getMessage(),
"logger": record.name,
"module": record.module,
"line": record.lineno
}
if record.exc_info:
log_entry["exception"] = self.formatException(record.exc_info)
# Add extra fields
for key, value in record.__dict__.items():
if key not in {"name", "msg", "args", "levelname", "levelno",
"pathname", "filename", "module", "exc_info",
"exc_text", "stack_info", "lineno", "funcName",
"created", "msecs", "relativeCreated", "thread",
"threadName", "processName", "process"}:
log_entry[key] = value
return json.dumps(log_entry)
# Configure logging — stdout only (no files in containers)
logger = logging.getLogger("app")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(JSONFormatter())
logger.addHandler(handler)
# Usage
logger.info("Request received", extra={
"method": "GET",
"path": "/api/users",
"request_id": "abc123",
"duration_ms": 42
})
# Docker logging drivers
# json-file (default) — with rotation
docker run --log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
myapp:latest
# syslog
docker run --log-driver syslog \
--log-opt syslog-address=tcp://logserver:514 \
myapp:latest
# fluentd
docker run --log-driver fluentd \
--log-opt fluentd-address=fluentd:24224 \
--log-opt tag=docker.myapp \
myapp:latest
# journald
docker run --log-driver journald myapp:latest
The default json-file driver keeps logs on the host without rotation — a chatty container will fill the disk in days. Always set max-size/max-file, or ship to fluentd/syslog and let the aggregator own retention. For metrics and alerting on top of logs, see the monitoring and alerting guide.
Secrets in Docker
Environment variables are the lazy path and the wrong one: docker inspect, /proc/<pid>/environ, crash dumps, and logs all leak them. And never bake secrets into the image via ARG or ENV — docker history exposes every layer’s build arguments, so a build-time secret survives in the image forever.
Two safe patterns: BuildKit secret mounts for build time, and mounted secret files for runtime.
# BuildKit secrets — never lands in a layer
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN=$(cat /run/secrets/npm_token) npm ci
# Build with:
# DOCKER_BUILDKIT=1 docker build --secret id=npm_token,src=token.txt .
# docker-compose.yml with secrets
version: "3.9"
services:
api:
image: myapp:latest
secrets:
- db_password
- api_key
environment:
DB_PASSWORD_FILE: /run/secrets/db_password
API_KEY_FILE: /run/secrets/api_key
secrets:
db_password:
file: ./secrets/db_password.txt
api_key:
file: ./secrets/api_key.txt
# Read secrets from files (Docker secrets)
import os
def read_secret(name: str) -> str:
"""Read a secret from the Docker secrets mount."""
path = os.environ.get(f"{name}_FILE", f"/run/secrets/{name.lower()}")
with open(path) as f:
return f.read().strip()
# Usage
db_password = read_secret("DB_PASSWORD")
api_key = read_secret("API_KEY")
For real secret lifecycle — rotation, audit, dynamic secrets — a secrets manager (Vault, AWS Secrets Manager) beats files; see the secrets management guide.
Multi-Architecture Builds
Apple Silicon laptops build linux/arm64 images; most servers run linux/amd64. docker buildx with QEMU cross-compiles a multi-arch manifest so the same tag serves both.
# Create buildx builder for multi-arch
docker buildx create --name multiarch --use
# Build for multiple architectures
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t myapp:latest \
--push \
.
# Inspect manifest
docker buildx imagetools inspect myapp:latest
# Dockerfile with architecture-specific logic
FROM --platform=$BUILDPLATFORM node:20-slim AS builder
ARG TARGETPLATFORM
ARG TARGETARCH
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM --platform=$TARGETPLATFORM node:20-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
$BUILDPLATFORM/$TARGETARCH let stages differ — build natively on the host architecture, ship to the target one. Watch for arch-specific native dependencies (prebuilt wheels/blobs that don’t exist for arm64) — they fail at build time under QEMU, which is slow but honest.
Runtime Security
Hardening happens at build time (small base, non-root user) and at run time (capabilities, filesystem, syscalls). Layer them:
# Secure Dockerfile — defense in depth
FROM python:3.12-slim
# Install only required system packages, then clean up
RUN apt-get update && \
apt-get install --no-install-recommends -y curl ca-certificates && \
rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN groupadd -r appuser && useradd -r -g appuser -s /bin/false appuser
WORKDIR /app
# Copy with correct ownership
COPY --chown=appuser:appuser requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY --chown=appuser:appuser . .
# Switch to non-root
USER appuser:appuser
# Set read-only filesystem (use --read-only at runtime)
# Set no-new-privileges (use --security-opt at runtime)
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
# Run with maximum security constraints
docker run -d \
--name api \
--read-only \
--tmpfs /tmp \
--security-opt no-new-privileges \
--security-opt seccomp=seccomp-profile.json \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
--network internal \
--cpus="2.0" \
--memory="1g" \
--pids-limit="4096" \
--restart unless-stopped \
myapp:latest
--cap-drop ALL then add back only what’s needed is the pattern — NET_BIND_SERVICE for ports under 1024, nothing else for most web apps. --read-only with --tmpfs /tmp gives the app a scratch space without a writable root filesystem. For the gold-standard level, rootless Docker or gVisor (runsc) puts a kernel between the container and the host. More patterns in the container security recipe.
Docker Compose for Production
# docker-compose.prod.yml
version: "3.9"
services:
api:
image: ghcr.io/myorg/myapp:${VERSION:-latest}
restart: unless-stopped
ports:
- "8000:8000"
environment:
- ENV=production
- LOG_LEVEL=info
deploy:
resources:
limits:
cpus: "2.0"
memory: 1G
reservations:
cpus: "0.5"
memory: 256M
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 10s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
security_opt:
- no-new-privileges:true
read_only: true
tmpfs:
- /tmp
networks:
- backend
worker:
image: ghcr.io/myorg/myapp-worker:${VERSION:-latest}
restart: unless-stopped
command: ["python", "-m", "celery", "-A", "tasks", "worker", "--loglevel=info"]
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
depends_on:
api:
condition: service_healthy
networks:
- backend
nginx:
image: nginx:1.25-alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./certs:/etc/nginx/certs:ro
depends_on:
api:
condition: service_healthy
networks:
- frontend
- backend
networks:
frontend:
driver: bridge
backend:
driver: bridge
internal: true # No external access
Note the network split: the backend network is internal, so only nginx can be reached from outside — the app and worker stay off the public network entirely.
Best Practices
- Pin base images by digest (
python:3.12-slim@sha256:…) — tags get repointed, digests don’t. - Order Dockerfile layers by change frequency — dependency installs before
COPY . .keeps the cache hot. - Write a real
.dockerignore— the build context shouldn’t carry.git, tests, ornode_modules. - Always set resource limits and restart policies — an uncapped container is an OOM kill waiting to happen.
- Run as non-root and drop all capabilities — the image is your last line of defense if the app is exploited.
- Health-check everything, but separate liveness from readiness — a dead database shouldn’t restart a healthy process.
- Scan in CI and fail on HIGH/CRITICAL — a report nobody gates on is a report nobody reads.
- Log structured JSON to stdout and rotate host logs — disk-full incidents are the most preventable outage there’s.
- Keep
docker-compose.prod.ymlseparate from dev compose — production config drifts from dev defaults fast.
Common Mistakes
- Secrets in
ARG/ENVor copied into the image:docker historyanddocker inspectexpose them permanently. Use BuildKit secret mounts and mounted files. latesttags in production: rebuilds become nondeterministic and rollbacks guesswork. Tag by git SHA or semver.- No health check: the container dies, nobody restarts it, and the first alert is a user report.
- Running as root: the default
USERis root — one missed line undoes most other hardening. docker logson the host with no rotation: the default json-file driver fills the disk; setmax-size/max-fileor a proper driver.- Alpine for Python/Node with native deps: musl libc breaks wheels and subtle edge cases — slim or distroless instead.
- JVM apps without
-XX:MaxRAMPercentage: the heap sizes itself against host RAM, then the cgroup limit OOM-kills the container anyway. - Full
docker buildcontext: no.dockerignoremeans every build ships.git— bigger images, slower builds, leaked history.
See Also
- Docker multi-stage build docs
- Distroless container images (GoogleContainerTools)
- Trivy image scanning docs
- CI/CD Security: Harden Your Pipelines and Prevent Supply
- Disaster Recovery: RTO, RPO, and Resilient Recovery Runbooks
- Complete Guide to Secrets Management
- Complete Guide to Supply Chain Security
- Docker production companion on GitHub — Dockerfiles, compose, .dockerignore, and a Trivy scan script
Frequently Asked Questions
What is the difference between slim and distroless images?
Slim images are based on Debian but strip out documentation, headers, and development tools. They still include a shell and basic utilities. Distroless images contain only the runtime (e.g., Python interpreter) and your application — no shell, no package manager, no utilities. Distroless is more secure (smaller attack surface) but harder to debug (no shell). Use distroless for production, slim for development.
How do I debug a distroless container?
Use the :debug variant of distroless images, which includes a busybox shell. Or use docker cp to copy debugging tools into a running container. For production debugging, rely on structured logs, distributed tracing, and metrics rather than shell access. You can also use ephemeral debug containers with kubectl debug in Kubernetes.
What resource limits should I set?
Start with CPU and memory limits based on your application's normal usage. Set memory limit to 1.5x the average memory usage to allow for spikes. Set CPU limit to the maximum your app should consume. Always set memory reservation lower than the limit to ensure the container gets at least that much. Set PID limits to prevent fork bombs. Test under load and adjust based on actual usage patterns.
How do I handle secrets in Docker without environment variables?
Use Docker secrets (Swarm mode) or mount secret files from a secrets manager. Read secrets from files at startup, not from environment variables. Environment variables are visible in docker inspect, process listings, and crash dumps. File-based secrets are only accessible to the container they're mounted in. For Kubernetes, use Sealed Secrets or External Secrets Operator.
Should I use Alpine or Debian-based images?
Use Debian-slim or distroless for production. Alpine uses musl libc instead of glibc, which can cause compatibility issues with Python packages (especially those with C extensions like numpy, pandas). The size difference between Alpine and slim is small (50MB vs 150MB), but the compatibility risk with Alpine is significant. Use Alpine only for Go or Rust binaries that are statically linked.
How do I reduce Docker image size?
Use multi-stage builds to exclude build tools from the final image. Use distroless or slim base images. Remove cache files: pip install --no-cache-dir, npm ci (not npm install). Combine RUN commands to reduce layers. Use .dockerignore to exclude test files, documentation, and build artifacts. Squash layers with docker build --squash (experimental) or use multi-stage builds. Scan with dive to analyze layer sizes.
What is the difference between HEALTHCHECK and a Kubernetes probe?
Docker's HEALTHCHECK marks a container healthy or unhealthy — Docker Compose and docker ps report it. Kubernetes ignores HEALTHCHECK and uses its own liveness/readiness probes in the pod spec, which restart containers or pull them out of the service endpoints. On Kubernetes you need the probes; HEALTHCHECK alone buys you nothing.
Related Resources
Complete Guide to Kubernetes Networking
Understand how Kubernetes networking really works: flat pod network, Service types, Ingress, NetworkPolicies, CNI plugins, DNS, and service mesh.
GuideComplete Guide to Monitoring and Alerting
Build a production monitoring stack with Prometheus, Grafana, and AlertManager — RED/USE methods, alert design, SLOs with error budgets, and runbooks.
GuideComplete Guide to Supply Chain Security
Secure your software supply chain end-to-end: SBOMs, dependency scanning, Sigstore signing, SLSA provenance, and CI/CD hardening with runnable examples.
RecipeContainer Security: Scan, Harden, and Sign Docker Images
Scan container images for CVEs and secrets with Trivy and Grype. Harden Dockerfiles, generate SBOMs, sign with Cosign, and gate deployments.
GuideComplete Guide to Terraform in Production
Operate Terraform safely in production: remote state with native locking, environment isolation, drift detection, CI/CD pipelines that apply reviewed plans, and policy as code.
GuideDocker Compose: Multi-Service Local Development
Master Docker Compose for local development: multi-service environments, networking, volumes, profiles, overrides, hot reload, debugging, test stacks, and the helper scripts that make it all pleasant to use.