Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications.The docker-compose.yml File
Compose uses a YAML file to configure your application’s services.
Complete Example
Real-World Architecture Example
A typical production setup with Nginx Reverse Proxy, Backend API, Redis Cache, and PostgreSQL Database.Key Concepts
Services
The computing resources of your application (containers).build: Build an image from a Dockerfile.image: Use an existing image from a registry.depends_on: Control startup order.
Environment Variables
- Inline: Defined directly in YAML.
- .env file: Compose automatically reads variables from a
.envfile in the same directory.
Networking
By default, Compose sets up a single network for your app.- Services can reach each other by service name (e.g.,
webcan pingdb).
Essential Commands
Production vs Development
Overriding Configuration
You can use multiple Compose files to handle different environments.docker-compose.yml: Base config.docker-compose.prod.yml: Production overrides (restart policies, extra volumes).
Key Takeaways
- Use Docker Compose for local development and testing.
- Use
.envfiles to manage secrets and configuration. - Use
depends_onwith healthchecks to ensure services start in the correct order. - Use Volumes to persist database data.
Next: Docker Best Practices →