Module 6 Lesson 2: Scaling Services in Compose
·DevOps

Module 6 Lesson 2: Scaling Services in Compose

More power. Learn how to use the --scale flag to run multiple instances of a service and how Docker Compose manages the load balancing internally.

Module 6 Lesson 2: Scaling Services in Compose

In standard Docker, you run one container. In Docker Compose, you can run an entire cluster of identical containers for a specific service. This is called Scaling.

1. The --scale Flag

If you have a service named web, you can start 5 copies of it with one command:

docker-compose up -d --scale web=5

What happens?

  1. Docker starts 5 separate containers.
  2. Each is given a unique name (e.g., folder_web_1, folder_web_2, etc.).
  3. Each is given its own internal IP address.

2. The Port Mapping "Trap"

If your docker-compose.yml says -p 80:80, you CANNOT scale it.

  • Why?: Two containers cannot use the same host port (80) at the same time.
  • The Fix: Remove the host port mapping (- 80) or use a Load Balancer (Review Module 10).

3. Replicas in the YAML (V3.8+)

Instead of using the command line flag, you can "Declare" the scalar in your file:

services:
  worker:
    image: my-worker
    deploy:
      replicas: 3

Note: This specific deploy setting is primarily for Docker Swarm, but modern Docker Compose versions also respect it.


4. Why Scale?

  1. Redundancy: If worker_1 crashes, worker_2 and worker_3 keep processing data.
  2. Performance: You can spread a heavy task (like image processing) across 10 containers to finish it 10x faster.
  3. No-Downtime Rebuilds: When you update, Docker can replace the scaled containers one-by-one.

Exercise: The Worker Herd

  1. Create a Compose file with a service worker using the alpine image.
  2. Set the command to sleep 1000.
  3. Scale it to 5 instances.
  4. Run docker-compose ps. Are all 5 running?
  5. Run docker stats. How much total memory are all 5 using together?
  6. Try to scale it back down to 1. What does Docker do to the extra 4 containers?

Summary

Scaling is your first step into the world of High Availability. By running multiple instances of your application, you ensure that a single container crash doesn't mean a total service outage.

Next Lesson: Checking the pulse: Healthchecks and restart policies in Compose.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn