In the last episode, we learned how to use Docker Compose to define and run multi-container applications. Now, letβs take it a step further by exploring advanced Compose features that make managing complex applications easier.
1. Scaling Services
Sometimes, one container instance isnβt enough. Docker Compose allows you to scale services easily.
docker-compose up --scale web=3 -d
Here:
-
web=3
β Runs 3 replicas of theweb
service. - Great for load balancing and handling more traffic.
2. Service Dependencies
You can define dependencies between services so that containers start in the right order.
Example in docker-compose.yml
:
version: '3'
services:
db:
image: postgres:latest
app:
build: ./app
depends_on:
- db
-
depends_on
ensures the database container starts before the app container.
β οΈ Note: It doesnβt wait until the DB is fully ready. For that, use healthchecks.
3. Override Configurations
Docker Compose allows multiple config files for different environments.
Default:
docker-compose -f docker-compose.yml up
Override with dev/test/prod:
docker-compose -f docker-compose.yml -f docker-compose.override.yml up
- This helps in switching configs without changing the base file.
4. Healthchecks
Make sure containers are healthy before others start depending on them.
version: '3'
services:
db:
image: postgres:latest
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 10s
retries: 5
This ensures the database is actually ready before other containers try connecting.
5. Tips for Advanced Compose Usage
- Use
.env
files to manage environment variables. - Separate dev and prod Compose files.
- Use scaling + reverse proxy (like Nginx/Traefik) for load balancing.
π Hands-on Challenge
- Create a
docker-compose.yml
withnginx
,flask
, andpostgres
. - Scale Flask service to 3 replicas.
- Add a healthcheck for Postgres.
- Run and verify scaling + dependencies.
β In the next episode, weβll move into Docker Networking (Connecting Containers Across Projects).
Top comments (0)