[英]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.