Skip to content

Scaling a service: three api containers

Select an icon to see what it is.

Step 6 of 6

Scaling down removes replicas

docker compose up -d --scale api=1 stops and removes the extra replicas. Compose normally removes the highest-numbered ones first, so shop-api-1 stays, but this order is not guaranteed. A reverse proxy such as nginx in front is the usual way to share traffic between replicas properly.

$ docker compose up -d --scale api=1
PrevNext

Calls sent by Compose

POST /containers/shop-api-3/stop
DELETE /containers/shop-api-3
then the same for shop-api-2

Try it yourself

Scale api to three, look it up by name, then scale it back down.

$ cat > compose.yaml <<'EOF' name: shop services: web: image: nginx ports: - "8080:80" networks: default: ipv4_address: 172.18.0.2 api: image: traefik/whoami command: ["--port", "3000"] networks: default: name: shop-net ipam: config: - subnet: 172.18.0.0/16 EOF
$ docker compose up -d --scale api=3
$ docker compose ps
$ docker compose exec web getent ahostsv4 api
$ docker compose up -d --scale api=1
$ docker compose down

Quiz

1. What does --scale api=3 create?
2. Why does a fixed host port stop a service from scaling?
3. Does Compose balance load between replicas?