What is Docker Compose used for?
- Technology:
- Docker
Quick answer
Docker Compose defines a multi-container application (services, networks and volumes) in one YAML file so you can start, stop and rebuild the whole stack with a single command, typically for local development and testing.
Detailed explanation
Most applications need more than one container: an API, a database, a cache, perhaps a worker. Starting each with long docker run commands is error-prone. Compose describes them declaratively in compose.yaml.
Services on the same Compose network reach each other by service name, so the API connects to postgres://db:5432 rather than an IP address. depends_on with health checks controls start-up order, and environment variables can come from an .env file.
Compose is ideal for development, CI and small single-host deployments. For production across many machines, teams move to an orchestrator such as Kubernetes or a managed container service, which adds scheduling, self-healing and rolling updates.
Example
services:
api:
build: .
ports: ["3000:3000"]
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
volumes:
pgdata:Key points
- One file describes services, networks and volumes
- Services find each other by name
- Great for development, CI and single-host setups
- Kubernetes or managed services for multi-host production