简体   繁体   English

如何在没有--scale的情况下使用docker-compose指定容器数量?

[英]How to specify number of containers using docker-compose without --scale?

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. 在我的docker-compose我有多个客户端和工作程序类,特别是一个类型为A的客户端,一个类型为B的客户端,另一个是类型C的客户端,以及它们各自的工作器类。 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 [...] . 每次执行--scale docker-compose ,如果要为每个类使用多个不--scale cliA=2 --scale cliB=3 [...] 1的容器,则总共需要使用--scale总共6次:-- --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)? 除了在我docker-compose.yml上拥有类之外,还有其他替代方法,而是为客户端提供统一的类,该类对于每个不同的类(对于工作人员而言,相同)可以缩放吗?

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类参考):

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. 这个docker-compose.yml能够根据需要创建类,而无需每次都调用--scale However, I have checked the reference for docker-compose but I haven't found anything that helps me. 但是,我检查docker-compose的参考资料,但没有找到任何对我docker-compose东西。 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). 我找到了一篇很有见地的文章 ,其中提到我可以使用docker-swarm来完成此任务,但我认为这超出了主题的范围(此问题试图回答一个练习)。

Here is the code for the docker-compose.yml file: 这是docker-compose.yml文件的代码:

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. 任何帮助表示赞赏。

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, eg: 您可以使用Yaml别名和锚点语法将一个服务有效地复制到另一个服务,然后应用更改,例如:

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. 我也看到了在撰写文件中使用变量的可能性。 Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在docker-compose scale之后通过主机名访问其他容器? - How to reach additional containers by the hostname after docker-compose scale? docker-compose 规模容器上的警告 - docker-compose warning on scale containers 使用docker-compose扩展服务时如何在卷路径中指定迭代器? - How to specify an iterator in the volume path when using docker-compose to scale up service? 如何在没有“服务容器”的情况下连接到在 Github Workflow 中使用 docker-compose/docker 启动的服务? - How to connect to service started using docker-compose/docker in Github Workflow without 'service containers'? 如何从泊坞窗获取数字-向上扩展 - How to get number from docker-compose up --scale 如何指定在 docker-compose 中启动和不启动哪些容器? (docker run vs docker create) - How would one specify which containers to start and not to start in docker-compose? (docker run vs docker create) 如何使用docker-compose链接两个容器 - How to link two containers using docker-compose 使用docker-compose时如何修复容器的基本名称? - How to fix basename of containers when using docker-compose? docker-compose 如何使用同一个镜像创建多个容器 - docker-compose how to create multiple containers with using the same image 如何使用正则表达式在docker-compose文件中指定docker镜像 - How to specify a docker image in docker-compose file using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM