157

I need to start multiple containers for the same image. If I create my compose file as shown below, it works fine.

version: '2'

services:
  app01:
    image: app
  app02:
    image: app
  app03:
    image: app
  app04:
    image: app
  app05:
    image: app

Is there an easy way for me to mention the number of instances for the compose instead of copy and pasting multiple times?

5 Answers 5

193

Updated answer (Jul 2023)

The old "scale" feature is now called replicas and it is part of the current Docker Compose specs, 2.19.1 as of writing.

Note that replicas is ignored if you name your container using container_name: myname. You must let the Docker generate the names.

services:
  myapp:
    image: awesome/webapp
    deploy:
      mode: replicated
      replicas: 6

Updated answer (Oct 2017)

As others mentioned, the Docker API has changed. I'm updating my answer since it's the one most people will probably look at.

docker-compose up -d --scale app=5

Unfortunately, we cannot specify this in a docker-compose.yml file currently as of version 3.5 (1.18.0 in re-numbered version).

Details:
They did introduce the scale option for version 2.2 and 2.3 of docker-compose, but they removed it for version 3.0. Also, to use version 2.2 or 2.3 you would need to download an older version of the docker-compose tool. The current version does not support 2.2 or 2.3 (it does support 2.0 or 2.1 however). There is also a new deploy section with replicas: 5 but it's only for swarm mode.


Old Answer

docker-compose scale app=5

See docker compose up.

Then you only need this docker-compose file:

version: '2'

services:
  app:
    image: app
9
  • 11
    Is there a way to specify this in the docker-compose file, so that when you run docker-compose up, it will run the preconfigured number of instances?
    – augustomen
    Commented Oct 13, 2017 at 14:09
  • 2
    How do you set the port range for the containers? Commented Mar 11, 2018 at 14:13
  • 1
    @MuhammadAbrar You specify the ports the same as with any service. Either use expose: for exposing ports to other services on the same network or ports: to expose ports to the host (publicly accessible). docs.docker.com/compose/compose-file/#short-syntax-1
    – Bernard
    Commented Mar 13, 2018 at 7:53
  • 1
    Notice that omitting -d (daemon mode) means all containers started using the same docker-compose file will stop once Ctrl+C is used to terminate the last scaled container. Very dangerous. I'd suggest adding -d to docker-compose up in the answer above, to avoid copy/paste disasters. Commented Mar 28, 2019 at 13:22
  • 1
    @MarcoChiappetta good point. I usually do up -d. Typo this time.
    – Bernard
    Commented Mar 29, 2019 at 1:26
78

You can do it with replica as mentioned in Compose specification:

version: '3'
services:
  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 6

One can use docker-compose --compatibility up to make Docker accept a deploy section without using swarm.

12
  • 36
    Replicas is only for deploying in a swarm.
    – Ken J
    Commented Jul 17, 2017 at 1:28
  • 3
    Yes, if the option is ignored by ordinary docker-compose up commands. Commented Jan 19, 2018 at 15:13
  • 3
    The deploy intro/header docs state that the entire section is ignored by docker-compose up (docs.docker.com/compose/compose-file/#deploy)
    – hcarty
    Commented Jan 26, 2018 at 0:33
  • 43
    One can use docker-compose --compatibility up to make docker accept deploy section without using swarm. Commented Oct 8, 2018 at 19:12
  • 4
    @NikolaPetkanski your comment should be marked as the correct answer. Thanks!
    – Rinor
    Commented Feb 10, 2019 at 21:41
44

The scale command is now deprecated, and you should use up instead.

docker-compose up --scale app=2

More details are on docker compose up.

3
  • This is actually problematic and it's not covered in the doc. If we have services A, B, C and we do docker-compose up --scale B=2, it's going to start all A, B, C, so it's equivalent to docker-compose up --scale A=1 --scale B=2 --scale C=1. I'm pretty sure this is not what the user wants in many cases.
    – Shiva Wu
    Commented Nov 7, 2018 at 21:10
  • 1
    @fssilva, This command worked in my case. However I have to modify my compose file. (1) remove container name from compose file to resolve "name is already in use by container' error. (2) remove explicit port bindings to resolve "failed: port is already allocated" error. Commented Jul 13, 2020 at 7:38
  • 1
    @ShivaWu in that case specify the desired service at the end e.g.docker-compose up --scale appB=2 appB to only start service B. I normally do docker-compose up then if I want additional instances of a particular service I follow up with docker-compose up --scale appB=5 appB
    – Gostega
    Commented Jan 14, 2021 at 0:14
14

You can do this:

version: "3.4"

services:
  service1: &service_1
    image: app

  service2:
    <<: *service_1

  service3:
    <<: *service_1

For more information on <<, see What is the << (double left arrow) syntax in YAML called, and where's it specified?.

2
  • 1
    This tricky basically defined multiple services. Only applicable for a worker container, if your container provide service to others, supposed to expose port, the other containers will not able to connect via service discovery.
    – Dai
    Commented Apr 13, 2022 at 6:46
  • If you are using multiple docker-compose files (like when calling docker-compose with the -f flag or setting the COMPOSE_FILE env variable), the anchor must be defined in the same file you are referencing it, can't have them splitted in different files. Commented Jun 6, 2023 at 17:38
3

Works for me well:

version: "3.9"

services:
  web:
    image: redis:6.2-alpine
    ...
    deploy:
      mode: replicated
      replicas: 3

and run the command then:

docker-compose --compatibility up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.