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?
- Docker starts 5 separate containers.
- Each is given a unique name (e.g.,
folder_web_1,folder_web_2, etc.). - 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?
- Redundancy: If
worker_1crashes,worker_2andworker_3keep processing data. - Performance: You can spread a heavy task (like image processing) across 10 containers to finish it 10x faster.
- No-Downtime Rebuilds: When you update, Docker can replace the scaled containers one-by-one.
Exercise: The Worker Herd
- Create a Compose file with a service
workerusing thealpineimage. - Set the command to
sleep 1000. - Scale it to 5 instances.
- Run
docker-compose ps. Are all 5 running? - Run
docker stats. How much total memory are all 5 using together? - 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.