Learn Docker Compose by Examples
Compose Specification v2Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes.
These examples cover the core concepts of Docker Compose, enabling you to orchestrate complex applications with ease. You will learn how to define services, manage networking, handle persistent storage, and configure environment variables.
Service Definitions
Defining multi-container applications with this sample code demonstrating services block structure, image versus build specification, container naming, restart policies, and automatic DNS resolution for inter-service communication.
Volume Persistence
Managing data persistence with this code example showing named volumes managed by Docker, bind mounts mapping host directories, read-only mount options, and local driver for volume storage.
Network Segmentation
Isolating services with custom networks through this sample code demonstrating network definition with bridge driver, service network assignment, internal network isolation, and implementing least privilege networking.
Environment Configuration
Configuring runtime variables with this code example showing environment block with map syntax, array syntax alternative, variable substitution from host shell, and loading variables from external env files.
Service Dependencies
Controlling startup order with this sample code demonstrating depends_on for basic dependencies, service_healthy condition for waiting on healthchecks, service_started condition, and eliminating wait-for-it scripts.
Port Exposure
Mapping container ports to host with this code example showing HOST:CONTAINER syntax, binding to specific host interfaces, port range mapping, protocol specification for UDP, and security considerations.
Restart Policies
Configuring container restart behavior with this sample code demonstrating no restart default, on-failure for error-only restarts, always for aggressive restarts, and unless-stopped respecting manual stops.

