DEV Community

Yash Sonawane
Yash Sonawane

Posted on

Docker Series: Episode 17 - Advanced Docker Compose (Scaling, Dependencies, Overrides)

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
Enter fullscreen mode Exit fullscreen mode

Here:

  • web=3 β†’ Runs 3 replicas of the web 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Override with dev/test/prod:

docker-compose -f docker-compose.yml -f docker-compose.override.yml up
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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

  1. Create a docker-compose.yml with nginx, flask, and postgres.
  2. Scale Flask service to 3 replicas.
  3. Add a healthcheck for Postgres.
  4. Run and verify scaling + dependencies.

βœ… In the next episode, we’ll move into Docker Networking (Connecting Containers Across Projects).

Top comments (0)