Docker Community Forums

Share and learn in the Docker community.

Pass incremental variable to CMD for each scaled service in docker-compose


(Peetonn) #1

My docker-compose.yml looks like follows:

version: '2'
services:
  consumer:
    build: 
      dockerfile: Dockerfile-consumer
    container_name: client2
    command: ["/root/headless/runtest.sh", "client2", "hub", "/root/headless/config/test.cfg", $RUNTIME, $TAGS]
    depends_on:
     - hub
     - producer
    networks:
     - local-testbed
  producer:
    build: 
      context: client
      dockerfile: Dockerfile-producer
    container_name: client1
    command: ["/root/headless/runtest.sh", "client1", "hub", "/root/headless/config/test.cfg", $RUNTIME, $TAGS]
    depends_on:
     - hub
    networks:
     - local-testbed
  hub:
    build: hub
    container_name: hub
    command: ["/root/headless/runtest.sh", "hub", "20", $RUNTIME, $TAGS]
    networks:
     - local-testbed
networks:
  default:
    ipam:
      config:
       - subnet: 10.10.11.0/24
  local-testbed:
    driver: overlay
    ipam:
      config:
       - subnet: 10.10.12.0/24

I need to run multiple consumers. However, I can’t just simply type “docker-compose scale consumer” as it’ll launch container with the same exact command with second argument being “client2”, whereas I need this variable to have unique value and sequential number for new consumers, like client3, client4, client5, etc.

Without resorting to a script for generating compose files, is there a way/workaround to achieve this using standard docker-compose syntax?