4

Docker compose allows for static stack description. But what if I have a set of containers based on same image but different by subtle environment configuration? Moreover, I don't need them all at once, they can be launched and disposed of sequentially.

A quick shot would be to copy-paste the service, but a couple of dozens of them is a waste of resources.

Is there sort of elegant orchestration loop in this case?

UPD After some more googling, interesting fact came out that apparently what I do is batch job processing which has currently no conceptual (!) support in Docker compose because services are not batch jobs. While there is a proposed feature for that I will try out one of the community workarounds, Jobs As A Service (JaaS) but my question is still - maybe there is an elegant way to do the following:

version: '3.1'

services:

  task1:
    image: myjob
    environment:
      PARAM: foo

  task2:
    image: myjob
    environment:
      PARAM: bar

You may have asked yourself "why not just take a series of docker run/exec/rm", but the rest of the environment like database runs in the stack.

Ta Mu
  • 6,772
  • 5
  • 39
  • 82

1 Answers1

2

Correct me if I am wrong, but that is one of the reasons why docker orchestration tools exists like docker-swarm to be able to deploy multiple docker containers by using one command line.

Create a service with 5 replica tasks (–replicas)

Use the --replicas flag to set the number of replica tasks for a replicated service. The following command creates a redis service with 5 replica tasks:

$ docker service create --name redis --replicas=5 redis:3.0.6

4cdgfyky7ozwh3htjfw0d12qv

If for example 1000 identical containers that use a different environment variable have to be deployed, multiple docker service create could be added to a script, instead of copy-paste multiple times in one docker-compose file.

030
  • 13,235
  • 16
  • 74
  • 173