Lesson content
Read, practise, then check your understanding
Docker Compose
Docker Compose reads a YAML application model and reconciles related services, networks, volumes, secrets, and configuration on one Docker environment. It is valuable for local development, integration environments, and small controlled deployments.
Core ideas
- A service describes an image or build plus runtime configuration and desired replicas.
- Compose creates a default network where services resolve one another by service name.
- Top-level named volumes preserve state independently of a particular service container.
depends_oncontrols creation order; a health condition can delay a dependent service until readiness.
How it works
Keep the base Compose file environment-neutral and apply reviewed overrides or environment files for local differences. Use docker compose config to inspect the fully resolved model before starting it. Rebuild or pull deliberately, then use up -d, logs, ps, and down for lifecycle.
Command or configuration example
services:
api:
build: .
ports: ["8080:8080"]
environment:
DATABASE_URL: postgres://app:app@db/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:17-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
volumes: [db-data:/var/lib/postgresql/data]
healthcheck:
test: [CMD-SHELL, "pg_isready -U app"]
interval: 5s
retries: 10
volumes:
db-data:
Run examples first in a disposable environment. Replace image names, versions, credentials, ports, paths, and resource values with reviewed settings appropriate to your system.
Production guidance
- Do not commit production passwords in Compose YAML; use an external secret mechanism.
- Pin images, define health checks, and set resource and logging policy for shared environments.
- Understand that Compose is an application model, not automatically a multi-host orchestrator.
The chapter quiz follows this lesson and checks the concepts as well as the operational tradeoffs.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.