World map

 

BM: Baca dalam Bahasa Melayu

World D4: Set Meals

Scale a service

You will learn: to run more copies of one service, and when you can't.

Checked with Docker Engine 29 on 31 Oct 2026

The problem

Lunch rush! One API cook can’t keep up. You want three. What do you do?

Pick one answer.

Watch

Three api boxes, numbered api-1 to api-3, and one web box. Docker transcript: $ docker compose up -d --scale api=3 [+] up 5/5 ✔ Network kedai-kancil_default Created 0.1s ✔ Container kedai-kancil-web-1 Started 0.3s ✔ Container kedai-kancil-api-1 Started 0.3s ✔ Container kedai-kancil-api-2 Started 0.3s ✔ Container kedai-kancil-api-3 Started 0.3s $ docker compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS kedai-kancil-api-1 kedai-kancil-api "docker-entrypoint.s…" api 3 seconds ago Up 3 seconds 3000/tcp kedai-kancil-api-2 kedai-kancil-api "docker-entrypoint.s…" api 3 seconds ago Up 3 seconds 3000/tcp kedai-kancil-api-3 kedai-kancil-api "docker-entrypoint.s…" api 3 seconds ago Up 3 seconds 3000/tcp kedai-kancil-web-1 kedai-kancil-web "/docker-entrypoint.…" web 3 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp

Your laptop~/kedai-kancil

  • No open hatches

Lunchboxes (containers)

  • No containers yet

Frozen packs (images)

  • kedai-kancil-api:latest6 layers · 244MB
  • kedai-kancil-web:latest9 layers · 92.6MB
  • nginx:1.30-alpine8 layers · 93.5MB
  • node:24-alpine4 layers · 244MB
  • postgres:18-alpine9 layers · 433MB
  • redis:8-alpine7 layers · 162MB

0 containers (0 running), 6 images, 0 lockers.

To scale: Run more copies of one service: docker compose up -d --scale api=3. a service is to run more copies of it. --scale api=3 asks for three. Compose numbers them: api-1, api-2, api-3.

Try it

The rush is over. Keep just two api cooks.

Your laptop~/kedai-kancil

  • Laptop port 8080 → kedai-kancil-web-1 port 80

Lunchboxes (containers)

  • kedai-kancil-api-1running
    from kedai-kancil-api kedai-kancil_default
  • kedai-kancil-api-2running
    from kedai-kancil-api kedai-kancil_default
  • kedai-kancil-api-3running
    from kedai-kancil-api kedai-kancil_default
  • kedai-kancil-web-1running
    from kedai-kancil-web 8080→80 kedai-kancil_default

Frozen packs (images)

  • kedai-kancil-api:latest6 layers · 244MB · in use
  • kedai-kancil-web:latest9 layers · 92.6MB · in use
  • nginx:1.30-alpine8 layers · 93.5MB
  • node:24-alpine4 layers · 244MB
  • postgres:18-alpine9 layers · 433MB
  • redis:8-alpine7 layers · 162MB

Phone lines (networks)

  • kedai-kancil_defaultkedai-kancil-api-1 ↔ kedai-kancil-api-2 ↔ kedai-kancil-api-3 ↔ kedai-kancil-web-1

4 containers (4 running), 6 images, 0 lockers.

Predict

Now you scale web to two. Both copies want hatch 8080 on your laptop. What happens?

Pick one answer.

Check

web-2 is created, then fails: Bind for 0.0.0.0:8080 failed: port is already allocated. Docker transcript: $ docker compose up -d [+] up 3/3 ✔ Network kedai-kancil_default Created 0.1s ✔ Container kedai-kancil-web-1 Started 0.3s ✔ Container kedai-kancil-api-1 Started 0.3s $ docker compose up -d --scale web=2 [+] up 1/2 ✔ Container kedai-kancil-web-1 Running 0.0s ✘ Container kedai-kancil-web-2 Error 0.1s Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint kedai-kancil-web-2 (2655bb708b0080b740109467e2bba74d4c6f1f24f4f8140568f74733d145c28f): Bind for 0.0.0.0:8080 failed: port is already allocated

Your laptop~/kedai-kancil

  • No open hatches

Lunchboxes (containers)

  • No containers yet

Frozen packs (images)

  • kedai-kancil-api:latest6 layers · 244MB
  • kedai-kancil-web:latest9 layers · 92.6MB
  • nginx:1.30-alpine8 layers · 93.5MB
  • node:24-alpine4 layers · 244MB
  • postgres:18-alpine9 layers · 433MB
  • redis:8-alpine7 layers · 162MB

0 containers (0 running), 6 images, 0 lockers.

Real words: port is already allocated. Plain words: web-1 already holds hatch 8080.

Check

Can this dish scale on one laptop? Right for yes, left for no.

Swipe right for good, left for needs work. Or use the buttons.

1 / 4

api with no ports

api with no ports

You did it!

You can now run more copies of one service.

Go deeper (optional)

deploy: replicas: 3 in the file does the same as the flag. Asking for api on the phone line now finds all three boxes. Docker doesn’t truly share the work between them. A proxy does that, or Kubernetes in Part 3. Compose runs on one machine only.

Real rep (optional): in a project with an api service and no ports, run docker compose up -d --scale api=3, then docker compose ps.