Difficulty: Intermediate
How do you run multiple instances of a service with Docker Compose, and what problems do you have to handle when you do?
Run docker compose up -d --scale web=5 to start five containers of the web service, or set deploy.replicas: 5 in the Compose file so the count is version-controlled. Compose names them <project>-web-1 through -5, puts them all on the project network, and Docker's embedded DNS returns every replica's IP for the name web.
--scale or deploy.replicas (the CLI flag wins).web.web get all replica IPs from Docker's DNS server at 127.0.0.11, which gives basic round-robin distribution.Recent Compose v2 releases also accept docker compose scale web=5 as a standalone command.
ports: ["8080:3000"] means every replica tries to bind host port 8080, and the second one fails with "port is already allocated". Fixes: drop the host port and let Docker pick ("3000"), use a range ("8080-8084:3000"), or, best, publish only a reverse proxy and keep replicas internal.container_name. Container names must be unique, so a service with container_name set cannot be scaled at all. Remove it.Clients outside Docker cannot use the internal DNS, so you put Nginx, Traefik or Caddy in front, publish only its port, and point it at http://web:3000. One subtlety: Nginx resolves an upstream hostname once at startup, so replicas added later are not seen until reload unless you configure resolver 127.0.0.11 with a variable upstream. Traefik discovers containers dynamically through Docker labels and handles this for you.
| Need | Compose --scale | Swarm / Kubernetes |
|---|---|---|
| Multiple hosts | No, single machine | Yes |
| Auto-scaling on CPU or traffic | No | Yes (HPA in Kubernetes) |
| Rolling updates with health gating | Limited | Yes |
| Self-healing on node failure | No | Yes |
It is ideal for local load testing, reproducing race conditions between two workers, or running several queue consumers on one server. For example, a team might scale a Celery or BullMQ worker service to 4 replicas to drain an order queue faster during a sale.
Do not scale databases this way. Five Postgres containers sharing one named volume will corrupt data. Workers must also be safe to run in parallel: idempotent job handling and no in-memory session state that a sibling replica would not see.
services:
web:
build: .
expose:
- "3000" # internal only, no host port
deploy:
replicas: 3 # default count
proxy:
image: nginx:1.27-alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- web
`web` has no published port, so any number of replicas can run. The proxy is the single entry point.
docker compose up -d --scale web=5
docker compose ps web
# Every replica IP comes back for the service name
docker compose exec proxy nslookup web
`--scale` overrides deploy.replicas for this run, and the DNS lookup confirms all five replicas are registered under one name.
docker compose --scale, deploy.replicas, Port Conflicts, Embedded DNS, Load Balancing