docker-compose具有唯一环境变量的缩放 [英] docker-compose scaling with unique environment variable

查看:173
本文介绍了docker-compose具有唯一环境变量的缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在docker-compose文件中有一个示例计算服务,该服务按预期工作得很好。

I have a sample compute service in my docker-compose file which works just great as expected.

version: "3"
services:
  compute-service:
    image: dummy/compute
    environment:
      - INPUT=2

但是有时可能在其中我需要使用diff输入来运行此服务(例如INPUT = 4、7、9、10、12..etc)。我不喜欢为每个输入多次复制和粘贴服务的想法。缩放是一种选择。但是如何确保每个实例都可以在唯一的输入变量上工作。

However there could be times in which I need to run this service with diff inputs (say INPUT = 4, 7, 9, 10, 12..etc). I do not like the idea of copying and pasting the service multiple times for each input. Scaling is an option. But how can I ensure that each instance works on unique input variable.

我知道我可以使用这样的env变量。我的问题与如何传递唯一值有关,作为扩展的一部分!!

I am aware that I could use an env variable like this. My question is rather related to how to pass unique values as part of scaling!!

version: "3"
services:
  compute-service:
    image: dummy/compute
    environment:
      - INPUT=${INPUT}


推荐答案

使用 docker-compose 我不相信对此有任何支持。但是,在可以使用类似撰写文件的群集模式下,您可以使用 {{.Task.Slot}} 作为环境变量进行传递: //docs.docker.com/engine/reference/commandline/#create-services-using-templates rel = nofollow noreferrer>服务模板。您可以使用 docker swarm init 部署单节点集群。我将使用 docker stack deploy -c docker-compose.yml test docker-compose up 来部署以下示例c>。

With docker-compose, I don't believe there's any support for this. However, with swarm mode, which can use a similar compose file, you can pass {{.Task.Slot}} as an environment variable using service templates. You can deploy a single node swarm cluster with docker swarm init. Instead of docker-compose up, I'm deploying the following example with docker stack deploy -c docker-compose.yml test.

这是使用 {{。Task.Slot}} 功能的示例docker-compose.yml文件:

And here's an example docker-compose.yml file using the {{.Task.Slot}} functionality:

version: '3'
services:
  test:
    image: busybox
    command: /bin/sh -c "echo My task number is $$task_id && tail -f /dev/null"
    environment:
      task_id: "{{.Task.Slot}}"
    deploy:
      replicas: 5

然后,检查每个这些正在运行的容器:

Then, reviewing each of these running containers:

$ docker ps --filter label=com.docker.swarm.service.name=test_test
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
ccd0dbebbcbe        busybox:latest      "/bin/sh -c 'echo My…"   About a minute ago   Up About a minute                       test_test.3.i3jg6qrg09wjmntq1q17690q4
bfaa22fa3342        busybox:latest      "/bin/sh -c 'echo My…"   About a minute ago   Up About a minute                       test_test.5.iur5kg6o3hn5wpmudmbx3gvy1
a372c0ce39a2        busybox:latest      "/bin/sh -c 'echo My…"   About a minute ago   Up About a minute                       test_test.4.rzmhyjnjk00qfs0ljpfyyjz73
0b47d19224f6        busybox:latest      "/bin/sh -c 'echo My…"   About a minute ago   Up About a minute                       test_test.1.tm97lz6dqmhl80dam6bsuvc8j
c968cb5dbb5f        busybox:latest      "/bin/sh -c 'echo My…"   About a minute ago   Up About a minute                       test_test.2.757e8evknx745120ih5lmhk34

$ docker ps --filter label=com.docker.swarm.service.name=test_test -q | xargs -n 1 docker logs
My task number is 3
My task number is 5
My task number is 4
My task number is 1
My task number is 2

这篇关于docker-compose具有唯一环境变量的缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆