As your application grows, it often needs more than one container โ for example, a web server and a database. Managing them manually with separate Docker commands becomes painful.
Thatโs where Docker Compose comes in. It allows you to define and run multi-container Docker apps using a simple YAML file.
๐ง Why Docker Compose?
โ
Simplifies multi-container management
โ
One command to start/stop the entire stack
โ
Perfect for local development, testing, and CI pipelines
โ
YAML-based configuration โ easy to version control
๐ง What Youโll Learn in This Blog
- What Docker Compose is
- How to install and use it
- Writing a
docker-compose.yml
file - Real-world example: A web app with a database
- Useful Docker Compose commands
๐จ How to Install Docker Compose
Most modern Docker installations include Compose. To verify:
docker compose version
If not installed, follow: https://docs.docker.com/compose/install
๐ Example docker-compose.yml
Hereโs a basic example for a Python Flask app with a PostgreSQL database:
version: '3.8'
services:
web:
image: myflaskapp:latest
ports:
- "5000:5000"
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
โถ๏ธ How to Use Docker Compose
๐น Start the Services
docker compose up -d
๐น Stop the Services
docker compose down
๐น View Running Services
docker compose ps
๐น View Logs
docker compose logs -f
๐น Rebuild Images
docker compose up --build
๐ Real Use Case
Imagine you’re working on a Node.js API with a Redis cache. Instead of managing both separately, you define them in docker-compose.yml
and start them together.
This approach:
- Simplifies local development
- Mirrors your production stack
- Speeds up CI integration
๐ง Best Practices
- Use
.env
files to store secrets and environment variables - Name your services clearly
- Use volumes to persist database data
- Use
depends_on
to handle service order - Avoid running containers as root
๐ Internal Links
๐ External Resources
โ Conclusion
Docker Compose is a powerful tool for DevOps engineers. It simplifies working with multi-container applications, improves local testing, and fits perfectly into CI/CD pipelines.