Course contentsShow
← Cloud and Infrastructure
Lesson 1057 of 2,98920. Docker in DepthFree lesson

Scaling Services with Compose

Scale service replicas using the --scale flag and understand when scaling is appropriate in Compose.

Scaling Services with Compose

You'll learn how to horizontally scale containerized services using Docker Compose's --scale flag.

The Core Concept

Scaling a service means running multiple identical containers (replicas) of the same service to handle increased load. Docker Compose makes this trivially easy with the --scale flag. Think of it like a restaurant adding more chefs to the kitchen during rush hour—each chef follows the same recipe (your container image), but you have multiple working simultaneously.

How to Scale Services

When you run docker-compose up --scale web=3, Docker Compose starts three identical containers from the web service definition. Each replica is an independent container running the same image with the same configuration.

docker-compose up --scale web=3 --scale worker=5

This command launches 3 replicas of the web service and 5 replicas of the worker service.

When Scaling Makes Sense

Scaling is appropriate for:

  • Stateless services: Web servers, API endpoints, or worker processes that don't store local state
  • Load distribution: When a single container can't handle incoming traffic
  • High availability: Running multiple replicas protects against single-container failures
  • Background workers: Processing queue items or batch jobs in parallel

Important Limitations

You cannot scale services with hardcoded port mappings (ports: "8080:80") because multiple containers can't bind to the same host port. Use load balancers or remove explicit host port mappings when scaling.

Key Takeaway: Use docker-compose up --scale service=N to run multiple replicas of stateless services, enabling horizontal scaling and improved availability without code changes.