Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 934
KitKarson Avatar asked Sep 23 '16 14:09

KitKarson


People also ask

Can we execute multiple instances of Docker image on same server?

If you're expecting it to behave the same way, as when running a single container, it won't happen. If there's no specific reason for using version: '3' you may use version:'2' instead. Or you can let it create its own network, which it does with your current docker-compose file.

Can we create multiple containers from the same image?

None. A Docker container is built from one image.

How do I run multiple copies of a compose file on the same host?

πŸ”— Compose uses the project name to create unique identifiers for all of a project's containers and other resources. To run multiple copies of a project, set a custom project name using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.

Can multiple containers created on single Docker image?

Docker doesn't support mounting of different OS. Also, I cannot launch multiple OS containers from a single OS image.” Am I right? No, you can run Ubuntu docker image in RHEL Docker host or another other docker host. β€œLet say, I have an application image and this image doesn't contain any stuff related to guest OS.


1 Answers

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 
like image 66
Bernard Avatar answered Sep 28 '22 01:09

Bernard