Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

docker-compose creating multiple instances for the same image

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 any easy way for me to mention the number of instances for the compose instead of copy and pasting multiple times?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...