Scaling & Production
[!NOTE] This module explores the core principles of Scaling & Production, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. Horizontal Scaling
One of Docker’s superpowers is the ability to spin up multiple copies (replicas) of a service instantly.
# Start 3 copies of the 'worker' service
docker compose up --scale worker=3 -d
This is crucial for testing:
- Race conditions in your database.
- Session stickiness issues.
- Queue consumer throughput.
2. The Port Conflict Problem
If your docker-compose.yaml binds a specific host port:
services:
api:
ports:
- "8080:80" # Host:Container
You cannot scale this service. Why? Because only one process can listen on Host Port 8080. If you try to launch a second replica, it will crash with “Port already in use”.
The Solution: Use a Load Balancer
- Remove the
portsbinding from the app service. - Add a
proxyservice (Nginx/Traefik) that listens on Port 80. - Configure the proxy to distribute traffic to the app containers.
Interactive: Load Balancer Simulator
See how traffic is distributed across replicas.
👤
User
Nginx
Port 80
App 1
App 2
App 3
Waiting for traffic...
3. Code Example: Nginx Load Balancing
How to implement the simulator above in real code.
docker-compose.yaml:
services:
# The Application (scaled)
app:
image: my-app
# NO PORTS section! Internal only.
# The Load Balancer
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- app
nginx.conf:
events {}
http {
upstream myapp {
# Docker DNS resolves 'app' to MULTIPLE IPs!
# Nginx automatically round-robins between them.
server app:8080;
}
server {
listen 80;
location / {
proxy_pass http://myapp;
}
}
}
Now run:
docker compose up --scale app=3 -d
Nginx will automatically distribute traffic to all 3 containers.