Compose Scaling

Difficulty: Intermediate

Question

How do you run multiple instances of a service with Docker Compose, and what problems do you have to handle when you do?

Answer

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.

What Compose actually does

  1. Reads the desired replica count from --scale or deploy.replicas (the CLI flag wins).
  2. Creates or removes containers until the running count matches the target.
  3. Attaches each replica to the same networks with the alias web.
  4. Other services resolving 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.

The two things that break when you scale

  • Fixed host ports. 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.

Putting a load balancer in front

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.

What Compose scaling is not

NeedCompose --scaleSwarm / Kubernetes
Multiple hostsNo, single machineYes
Auto-scaling on CPU or trafficNoYes (HPA in Kubernetes)
Rolling updates with health gatingLimitedYes
Self-healing on node failureNoYes

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.

Gotchas with stateful services

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.

Scaled Compose service behind a proxy. Only the proxy publishes a host port; replicas are reached through Docker's internal DNS.
  1. Client hits localhost:80: The only published port belongs to the proxy.
  2. Nginx or Traefik proxy: Forwards requests to http://web:3000.
  3. Embedded DNS 127.0.0.11: Returns the IPs of every web replica.
  4. web-1 ... web-5: Identical containers on the project network.

Code examples

Scalable service with a proxy

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.

Scaling up and checking DNS

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.

Key points

  • Use --scale on the CLI or deploy.replicas in the file; the flag overrides the file.
  • Fixed host port mappings and container_name both prevent scaling.
  • Docker's embedded DNS returns all replica IPs for the service name.
  • Compose scaling is single-host and manual; there is no autoscaling.
  • Never scale stateful services like databases with --scale.

Common follow-up questions

  • Why does scaling fail with 'port is already allocated'?
  • How does Nginx keep track of replicas that are added later?
  • When would you move from Compose to Kubernetes?
  • How do you make a queue worker safe to run with multiple replicas?

Interview tips

  • Give the command, then immediately mention the port conflict; that is what interviewers are fishing for.
  • Describe the reverse proxy pattern with internal DNS to show you have built this, not just read about it.
  • Be honest that Compose is not an orchestrator and say when you would switch.

Concepts covered

docker compose --scale, deploy.replicas, Port Conflicts, Embedded DNS, Load Balancing

More Docker & Containers questions