HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Reuse of docker swarm containers for batch jobs

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
swarmdockerbatchjobsforreusecontainers

Problem

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.

Solution

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.

Code Snippets

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

4cdgfyky7ozwh3htjfw0d12qv

Context

StackExchange DevOps Q#1622, answer score: 2

Revisions (0)

No revisions yet.