Docker Compose: Multi-Container Apps
Real applications are rarely a single container, you usually need an app server, a database, maybe a cache. Docker Compose describes a whole multi-container setup in one file.
services:
web:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgresql://user:pass@db:5432/app
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: pass
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Running it
docker compose up -d
docker compose down
docker compose up builds (if needed) and starts every service defined in the file. Notice web reaches the database at the hostname db, Compose automatically creates a shared network where each service can find the others by service name.
depends_on controls startup order, but doesn't wait for the database to be fully ready to accept connections, that's a common real-world gotcha worth knowing about early.