docker-compose creating multiple instances for the same image

You can do it with replica as mentioned in https://docs.docker.com/compose/compose-file/#replicas

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

Edit (from the comments): One can use docker-compose --compatibility up to make docker accept deploy section without using swarm.


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

docker-compose up --scale app=2

more details in https://docs.docker.com/compose/reference/up


You can do this:

version: "3.4"

services:
  service1: &service_1
    image: app

  service2:
    <<: *service_1
    
  service3:
    <<: *service_1

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


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).

Details:
They did introduce the scale option for version 2.2 and 2.3 of docker-compose but 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 https://docs.docker.com/compose/reference/scale/

Then you only need this docker-compose file

version: '2'

services:
  app:
    image: app