KitKarson
KitKarson

Reputation: 5637

docker-compose scaling with unique environment variable

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

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.


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}

Upvotes: 17

Views: 12355

Answers (1)

BMitch
BMitch

Reputation: 263597

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.

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

Upvotes: 21

Related Questions