Overview
What is Docker By Example?
This tutorial teaches Docker containerization through 84 heavily annotated, runnable examples that achieve 95% coverage of Docker's features needed for production work. Each example is self-contained, copy-paste-runnable, and extensively commented to show exactly what happens at each step.
Who is This For?
Target audience: Experienced developers and DevOps engineers who prefer learning through working code rather than theoretical explanations.
Prerequisites:
- Familiarity with command-line interfaces
- Basic understanding of software development and deployment
- Experience with at least one programming language
What you'll learn:
- Dockerfile syntax and image building (multi-stage builds, layer optimization)
- Container lifecycle management (creation, execution, networking, volumes)
- Docker Compose orchestration (multi-container applications, service dependencies)
- Production patterns (health checks, resource limits, security best practices)
- Container registries and distribution (Docker Hub, private registries)
- CI/CD integration (automated builds, testing, deployment)
How to Use This Tutorial
Code-First Learning Philosophy
Show the code first, run it second, understand through interaction.
Each example follows this pattern:
- Brief explanation (2-3 sentences) - What and why
- Diagram (when helpful) - Visualize the concept
- Annotated code - Heavily commented with
# =>notation showing outputs and states - Key takeaway - The core insight distilled
Example Annotation Pattern
All examples use # => notation to show outputs, states, and intermediate values:
# Base image selection
FROM node:18-alpine # => Pulls official Node.js 18 image based on Alpine Linux (small footprint)
# Working directory setup
WORKDIR /app # => Sets /app as the working directory inside container
# Dependency installation
COPY package*.json ./ # => Copies package files to /app/ (leverages layer caching)
RUN npm ci # => Installs exact dependencies from package-lock.json
# => Creates a new layer with node_modules/
# Application code
COPY . . # => Copies all source files to /app/
# Container configuration
EXPOSE 3000 # => Documents that container listens on port 3000 (informational only)
CMD ["node", "server.js"] # => Default command when container startsSelf-Contained Examples
Every example is copy-paste-runnable within its chapter scope:
- Beginner examples: Completely standalone (no external dependencies)
- Intermediate examples: Assume basic Docker knowledge but include all necessary files
- Advanced examples: Assume fundamentals but remain fully runnable
You can copy any example and run it immediately without referring to previous examples.
95% Coverage: What It Means
Included in 95%
Core Docker features for production work:
- Image building (Dockerfile syntax, multi-stage builds, layer optimization)
- Container lifecycle (run, exec, logs, inspect, stop, remove)
- Networking (bridge, host, overlay networks, service discovery)
- Data persistence (volumes, bind mounts, tmpfs)
- Docker Compose (service definitions, dependencies, environment variables)
- Security (user permissions, secrets, scanning, best practices)
- Resource management (CPU/memory limits, health checks, restart policies)
- Registry operations (push, pull, authentication, private registries)
- Production patterns (logging, monitoring, orchestration basics)
Excluded from 95% (the remaining 5%)
Specialized or rare scenarios:
- Docker engine internals and architecture
- Kubernetes-specific features (covered in separate Kubernetes tutorials)
- Deprecated Docker features (Swarm mode details)
- Platform-specific advanced features (Windows containers specifics)
- Docker plugin development
- Low-level container runtime details (containerd, runc internals)
Tutorial Structure
Beginner (Examples 1-27) - 0-40% Coverage
Focus: Docker fundamentals and core workflow
Topics:
- Installation and hello world
- Dockerfile basics (FROM, RUN, COPY, CMD)
- Image management (build, list, tag, remove)
- Container basics (run, stop, remove, logs)
- Volumes and data persistence
- Basic networking
- Docker Compose introduction
Example count: 27 examples
Intermediate (Examples 28-54) - 40-75% Coverage
Focus: Production patterns and service orchestration
Topics:
- Multi-stage builds for optimization
- Docker Compose services and dependencies
- Health checks and restart policies
- Resource limits (CPU, memory)
- Logging strategies and monitoring
- Environment variable management
- Networking modes and custom networks
Example count: 27 examples
Advanced (Examples 55-84) - 75-95% Coverage
Focus: Production deployment and optimization
Topics:
- Docker Swarm basics (orchestration introduction)
- Security best practices (scanning, secrets, user permissions)
- Registry operations (Docker Hub, private registries, authentication)
- CI/CD integration (automated builds, testing pipelines)
- Production deployment patterns
- Performance optimization
Example count: 30 examples
Color-Blind Friendly Diagrams
All diagrams use a WCAG AA compliant color palette:
- Blue (#0173B2) - Primary elements, starting states
- Orange (#DE8F05) - Processing states, intermediate steps
- Teal (#029E73) - Success states, outputs
- Purple (#CC78BC) - Alternative paths, options
- Brown (#CA9161) - Neutral elements, helpers
Never: Red, green, or yellow (not color-blind accessible)
Navigation
Structure:
- Beginner - Examples 1-27 (fundamentals)
- Intermediate - Examples 28-54 (production patterns)
- Advanced - Examples 55-84 (expert mastery)
Getting Started
System Requirements:
- Docker Engine 20.10+ (Linux, macOS, Windows)
- Docker Compose 2.0+ (included with Docker Desktop)
- Terminal/command-line access
- 4GB RAM minimum, 8GB recommended
Installation:
- Linux: Docker Engine installation
- macOS/Windows: Docker Desktop
Verify installation:
docker --version # => Docker version 24.0.0 or higher
docker compose version # => Docker Compose version v2.20.0 or higherHow This Differs from Other Tutorials
Traditional tutorials: Explain concepts, then show small code snippets.
This tutorial: Shows complete, runnable examples first, with heavy annotations explaining what happens at each step. Every example can be copied and executed immediately.
Coverage approach: Achieves 95% feature coverage through systematic progression across 84 examples, ensuring you learn everything needed for production Docker work.
Ready to Start?
Begin with Beginner for Docker fundamentals, or jump to Intermediate if you already know basic Dockerfile syntax and container management.
Learning tip: Run every example. Docker is best learned by doing, not reading. Each example is designed to be executed, examined, and modified.
Examples by Level
Beginner (Examples 1–27)
- Example 1: Hello World
- Example 2: Running Interactive Containers
- Example 3: Simple Dockerfile
- Example 4: Installing Dependencies in Dockerfile
- Example 5: ARG for Build-Time Variables
- Example 6: ENV for Runtime Variables
- Example 7: LABEL for Image Metadata
- Example 8: Image Listing and Management
- Example 9: Container Lifecycle Management
- Example 10: Container Logs and Inspection
- Example 11: Executing Commands in Running Containers
- Example 12: Container Port Mapping
- Example 13: Named Volumes for Data Persistence
- Example 14: Bind Mounts for Development
- Example 15: tmpfs Mounts for Temporary Data
- Example 16: Bridge Network Basics
- Example 17: Container to Container Communication
- Example 18: Environment Variables from File
- Example 19: Docker Compose Basics
- Example 20: Docker Compose with Build Context
- Example 21: Docker Compose Environment Variables
- Example 22: Docker Compose Volumes
- Example 23: Docker Compose Depends On
- Example 24: Docker Compose Networks
- Example 25: Docker Compose Restart Policies
- Example 26: Docker Compose Profiles
- Example 27: Docker Compose Override Files
Intermediate (Examples 28–54)
- Example 28: Multi-Stage Build Basics
- Example 29: Multi-Stage with Build Arguments
- Example 30: Multi-Stage with Multiple Runtimes
- Example 31: Build-Time Secrets
- Example 32: Docker Compose Build Optimization
- Example 33: Health Checks in Docker Compose
- Example 34: Service Dependencies with Restart
- Example 35: Resource Limits - CPU
- Example 36: Resource Limits - Memory
- Example 37: Combined Resource Limits
- Example 38: Logging Drivers
- Example 39: Structured Logging
- Example 40: Log Aggregation with EFK Stack
- Example 41: Custom Bridge Networks
- Example 42: Network Troubleshooting Tools
- Example 43: Init Containers Pattern
- Example 44: Shared Volumes for Data Exchange
- Example 45: External Networks Integration
- Example 46: Running as Non-Root User
- Example 47: Environment Variable Management
- Example 48: Docker Compose Profiles for Conditional Services
- Example 49: Container Resource Monitoring
- Example 50: Docker System Prune and Cleanup
- Example 51: Docker Events Monitoring
- Example 52: Docker Compose Watch for Development
- Example 53: Blue-Green Deployment with Docker Compose
- Example 54: Canary Deployment Pattern
Advanced (Examples 55–84)
- Example 55: Docker Swarm Initialization
- Example 56: Docker Swarm Services
- Example 57: Docker Secrets Management
- Example 58: Read-Only Root Filesystem
- Example 59: Dropping Linux Capabilities
- Example 60: Image Scanning for Vulnerabilities
- Example 61: Distroless Images for Minimal Attack Surface
- Example 62: User Namespaces for Privilege Isolation
- Example 63: Security Scanning in CI/CD Pipeline
- Example 64: Private Docker Registry
- Example 65: CI/CD with GitHub Actions
- Example 66: Docker Stack Deployment
- Example 67: Docker Swarm Service Constraints
- Example 68: Docker Swarm Rolling Updates and Rollback
- Example 69: Docker Swarm Service Scaling
- Example 70: Docker Swarm Rolling Updates Strategy
- Example 71: Docker Swarm Secrets Management
- Example 72: Docker Swarm Overlay Network Configuration
- Example 73: Docker Swarm Health Checks and Auto-Restart
- Example 74: Distributed Tracing with Jaeger
- Example 75: AppArmor Security Profiles
- Example 76: Seccomp Security Profiles
- Example 77: Container Resource Quotas and Limits
- Example 78: Docker Registry Garbage Collection
- Example 79: Docker BuildKit Advanced Features
- Example 80: High Availability Docker Registry
- Example 81: Docker Notary (Image Signing Infrastructure)
- Example 82: Docker Image Vulnerability Remediation
- Example 83: Docker Resource Monitoring with cAdvisor and Prometheus
- Example 84: Docker Compose Production Deployment Best Practices
Last updated December 29, 2025