Devops

Docker Cheatsheet & Decision Guide

πŸ“‹ At a Glance

AspectDetails
PurposeQuick reference for daily Docker work
FormatCheatsheet + decision trees
UsageBookmark this page for quick lookups
ScopeCommands, Dockerfile, Compose, troubleshooting

🎯 How to Use This Guide

This is your go-to reference for:

  1. Quick lookups - Command syntax you use but forget
  2. Decision making - Which approach to use when
  3. Troubleshooting - Common issues and fixes
  4. Copy-paste snippets - Production-ready patterns

πŸ“– Docker Commands Cheatsheet

Container Lifecycle

BASH(26 lines)
Code
Loading syntax highlighter...

Image Management

BASH(17 lines)
Code
Loading syntax highlighter...

Network & Volume

BASH(16 lines)
Code
Loading syntax highlighter...

Compose Commands

BASH(22 lines)
Code
Loading syntax highlighter...

πŸ“ Dockerfile Patterns

Minimal Node.js Production

DOCKERFILE(23 lines)
Code
Loading syntax highlighter...

Minimal Java Production

DOCKERFILE(18 lines)
Code
Loading syntax highlighter...

Minimal Python Production

DOCKERFILE(18 lines)
Code
Loading syntax highlighter...

Minimal Go Production

DOCKERFILE(13 lines)
Code
Loading syntax highlighter...

🐳 Compose Snippets

Production-Ready Service

YAML(32 lines)
Code
Loading syntax highlighter...

Database with Health Check

YAML(22 lines)
Code
Loading syntax highlighter...

Development Override

YAML(19 lines)
Code
Loading syntax highlighter...

Nginx Reverse Proxy

YAML(18 lines)
Code
Loading syntax highlighter...

🌳 Decision Trees

Base Image Selection

                    Which base image?
                          β”‚
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚               β”‚               β”‚
      Need shell?     Minimal size?    Compatibility?
          β”‚               β”‚               β”‚
          β–Ό               β–Ό               β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  Yes    β”‚     β”‚ scratch β”‚     β”‚  Need    β”‚
    β”‚         β”‚     β”‚ (Go,    β”‚     β”‚  glibc?  β”‚
    β”‚ Alpine  β”‚     β”‚ Rust)   β”‚     β”‚          β”‚
    β”‚ (~5MB)  β”‚     β”‚ (~0MB)  β”‚     β”‚ Yes:slim β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚ No:Alpineβ”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Language-specific:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Language β”‚ Recommended                             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Node.js  β”‚ node:20-alpine (simple)                 β”‚
β”‚          β”‚ node:20-slim (if native modules fail)   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Java     β”‚ eclipse-temurin:21-jre-alpine           β”‚
β”‚          β”‚ gcr.io/distroless/java21 (no shell)     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Python   β”‚ python:3.12-slim                        β”‚
β”‚          β”‚ python:3.12-alpine (if no native deps)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Go       β”‚ scratch (static binary)                 β”‚
β”‚          β”‚ gcr.io/distroless/static (with certs)   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Rust     β”‚ scratch (static with musl)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Volume Type Selection

                    What type of data?
                          β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚                     β”‚
Persistent data?    Config/Code?         Temporary?
    β”‚                     β”‚                     β”‚
    β–Ό                     β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Named Volume β”‚    β”‚ Bind Mount  β”‚    β”‚   tmpfs     β”‚
β”‚             β”‚    β”‚             β”‚    β”‚             β”‚
β”‚ - Database  β”‚    β”‚ - Dev code  β”‚    β”‚ - Cache     β”‚
β”‚ - Uploads   β”‚    β”‚ - Configs   β”‚    β”‚ - Secrets   β”‚
β”‚ - Logs      β”‚    β”‚ - Certs     β”‚    β”‚ - Sessions  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Examples:
volumes:
  - pgdata:/var/lib/postgresql/data  # Named: persist
  - ./config:/app/config:ro           # Bind: share
  - type: tmpfs                       # Tmpfs: temp
    target: /tmp
    tmpfs:
      size: 100M

Network Mode Selection

                    Container networking?
                          β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚                     β”‚
Container-to-        Host network         No network
container?           required?            needed?
    β”‚                     β”‚                     β”‚
    β–Ό                     β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   bridge    β”‚    β”‚    host     β”‚    β”‚    none     β”‚
β”‚  (default)  β”‚    β”‚             β”‚    β”‚             β”‚
β”‚             β”‚    β”‚ Performance β”‚    β”‚ Security    β”‚
β”‚ Isolation   β”‚    β”‚ Port access β”‚    β”‚ Batch jobs  β”‚
β”‚ DNS names   β”‚    β”‚ No NAT      β”‚    β”‚             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Advanced:
networks:
  frontend:
    driver: bridge           # Default, with DNS
  backend:
    driver: bridge
    internal: true           # No external access

Deployment Strategy Selection

                    What's the priority?
                          β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚                     β”‚                     β”‚
Zero downtime?      Instant rollback?    Test in prod?
    β”‚                     β”‚                     β”‚
    β–Ό                     β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Rolling    β”‚    β”‚ Blue-Green  β”‚    β”‚   Canary    β”‚
β”‚  Update     β”‚    β”‚             β”‚    β”‚             β”‚
β”‚             β”‚    β”‚ 2x resourcesβ”‚    β”‚ Traffic %   β”‚
β”‚ Container   β”‚    β”‚ Switch LB   β”‚    β”‚ Monitor     β”‚
β”‚ by containerβ”‚    β”‚ Keep old    β”‚    β”‚ Gradual     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Compose config:
deploy:
  update_config:
    order: start-first     # For rolling updates
    parallelism: 1
    delay: 10s
    failure_action: rollback

πŸ”§ Troubleshooting Quick Reference

Exit Codes

CodeSignalMeaningCommon Cause
0-Normal exitApp finished
1-Application errorBug, config error
126-Permission deniedchmod needed
127-Command not foundMissing binary
137SIGKILLKilledOOM, docker kill
139SIGSEGVSegfaultMemory corruption
143SIGTERMTerminateddocker stop

Quick Diagnostics

BASH(22 lines)
Code
Loading syntax highlighter...

Common Issues

SymptomCheckFix
Exit 137docker inspect --format='{{.State.OOMKilled}}'Increase memory limit
"Connection refused"docker network inspectSame network? Service healthy?
"No such file"docker diffCheck volume mounts
Slow startHealth check start_periodIncrease start_period
Port conflictlsof -i :PORTChange host port
Permission deniedFile ownershipMatch UID/GID

βœ… Production Checklist

Dockerfile

β–‘ Use specific base image tag (not :latest)
β–‘ Multi-stage build (small final image)
β–‘ Non-root USER
β–‘ HEALTHCHECK defined
β–‘ .dockerignore present
β–‘ No secrets in image
β–‘ Minimal packages installed

Compose

β–‘ depends_on with condition: service_healthy
β–‘ Health checks on all services
β–‘ Resource limits defined
β–‘ restart: unless-stopped
β–‘ Logging configured with max-size
β–‘ stop_grace_period set
β–‘ Environment variables with defaults/required

Security

β–‘ read_only: true where possible
β–‘ security_opt: no-new-privileges:true
β–‘ cap_drop: ALL (add back only needed)
β–‘ No privileged containers
β–‘ Secrets not in environment variables
β–‘ Images scanned for vulnerabilities

Operations

β–‘ Monitoring (Prometheus/Grafana)
β–‘ Centralized logging
β–‘ Alerts configured
β–‘ Backup strategy for volumes
β–‘ Rollback procedure tested
β–‘ Documentation updated

πŸ“Š Quick Reference Tables

Resource Defaults

LanguageMemory StartMemory ProdCPU
Node.js256M512M-1G0.5-1
Java512M1G-2G1-2
Python128M256M-512M0.5-1
Go64M128M-256M0.25-0.5

Health Check Timing

Service TypeIntervalTimeoutRetriesStart Period
Web API30s10s330-60s
Worker60s30s310s
Database10s5s510-30s
Cache10s5s55s

Log Rotation

Environmentmax-sizemax-fileRetention
Development10m3-
Staging50m57 days
Production100m1030 days

🎯 Golden Rules

  1. One process per container - Keep containers focused
  2. Immutable images - Never modify running containers
  3. Environment for config - Build once, configure at runtime
  4. Stateless when possible - Store state in volumes/databases
  5. Health checks everywhere - Know when services are ready
  6. Log to stdout/stderr - Let Docker handle log management
  7. Start-first updates - Zero downtime deployments
  8. Non-root by default - Minimize attack surface
  9. Pin versions - Reproducible builds
  10. Monitor everything - Find issues before users do

πŸ“š Series Complete!

Congratulations on completing the Docker Compendium! You've learned:

PartTopic
0How to Use This Series
1-3Container Internals, Images, Build Process
4-7Networking, Optimization, Multi-Stage, Base Images
8-10Configuration, Resources, Volumes
11-13Logging, Security, Debugging
14-16Compose Deep Dive, Dependencies, Dev vs Prod
17-19CI/CD, Deployment, Monitoring
20This Cheatsheet
Next Steps:
  1. Build a production-ready Docker setup for your project
  2. Set up monitoring with Prometheus/Grafana
  3. Practice zero-downtime deployments
  4. Review and improve existing Dockerfiles

πŸ“… Review Schedule

Keep this cheatsheet handy:

  • Daily: Quick command lookups
  • Weekly: Review decision trees
  • Monthly: Audit production configs against checklist
  • Quarterly: Review series for deeper concepts

πŸ“š Series Navigation