0

In my docker-compose I have multiple client and worker classes, specifically a client of type A, one of type B and another of type C, with their respective worker classes. Every time I execute docker-compose I need to use the option --scale a total of 6 times if I want to use a number of containers different to 1 for each class: --scale cliA=2 --scale cliB=3 [...]. Is there an alternative to having classes on my docker-compose.yml and instead have an unified class for a client which could be scaled differently for each different class (and the same for the worker)?

I have reasoned about it, and I have come to the conclusion that it may be possible to do something like this (check the code at the end of the question for reference on the cli class):

cli:
  image: client
  // More stuff
  scale: 4
    environment:
      CLASSID=A
  scale: 2
    environment:
      CLASSID=B
  // [...]

This docker-compose.yml would be able to create classes as needed without the need of calling --scale every time. However, I have checked the reference for docker-compose but I haven't found anything that helps me. I found an insightful post which mentioned that I could use docker-swarm in order to accomplish this task, but I think it's out of the scope of the subject (this question is trying to answer an exercise).

Here is the code for the docker-compose.yml file:

version: '2'
services:
  cliA:
    image: client
    build: ./client/
    links:
      - bro
    environment:
      - BROKER_URL=tcp://bro:9998
      - CLASSID=A
  // Similar description for cliB, cliC; only CLASSID changes

  worA:
    image: worker
    build: ./worker/
    links:
      - bro
    environment:
      - BROKER_URL=tcp://bro:9999
      - CLASSID=A
  // Similar description for worB, worC; only CLASSID changes

  bro:
    image: broker
    build: ./broker/
    expose:
      - "9998"
      - "9999"

Any help is appreciated.

Le Sir Dog
  • 95
  • 1
  • 10
  • This question looks like a duplicate to me. Can you explain how your question is different from https://stackoverflow.com/q/39663096/596285 – BMitch Jan 07 '19 at 12:39
  • My question adds job classes to the topic whereas the OP from the question you linked (which I also linked on the statement) only wants to run a single image in multiple containers; I need to run an image in multiple containers but changing some parameters. – Le Sir Dog Jan 07 '19 at 14:03

2 Answers2

2

Services are a definition of how to run a container, along with all of the settings. If you need multiple containers running with different settings, you need different services. You can use the Yaml alias and anchor syntax to effectively copy one service to another and then apply changes, e.g.:

version: "3"
services:
  app1: &app1
    image: app
    environment:
      app: 1
  app2:
    <<*app1
    environment:
      app: 2

Once you have broken your problem into multiple services, you can follow the advices from your linked question.


I'm also seeing the possibility to use variables in your compose file. E.g.

version: '2'
services:
  cli:
    image: client
    build: ./client/
    links:
      - bro
    environment:
      - BROKER_URL=tcp://bro:9998
      - CLASSID=${CLASSID}
    scale: ${SCALE}

And then you could deploy with various environment files:

$ cat envA.sh
CLASSID=A
SCALE=4
$ cat envB.sh
CLASSID=B
SCALE=2
$ set -a && . ./envA.sh && set +a && docker-compose -p projA up
$ set -a && . ./envB.sh && set +a && docker-compose -p projB up
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • The second way was the one I was looking for, to unify different types of clients into one. I don't know if that is the answer the professors are thinking, but it definitely makes sense. Thanks! – Le Sir Dog Jan 07 '19 at 16:13
0

You can scale up or down your containers by the following command:

docker-compose up --scale service=1 -d

Here, service=1 specifies how may containers you want to run in daemon.

buddemat
  • 4,552
  • 14
  • 29
  • 49