Search code examples
dockerdocker-composeorchestration

Docker Compose activate container on demand


I have an application that performs elaboration over a data feed. The process is divided into tasks so I structured a docker-compose.yml file like this:

task1-service: 
  image: task1-image
task2-service: 
  image: task2-image
task3-service: 
  image: task3-image

Each task-service is triggered by the end of the previous and triggers the next, then it can exit. So there's no point to keep each service running.

I wonder if there's a solution to keep them all stopped, and start each service on demand when needed. I don't know if docker compose is the correct solution, but I like the idea ok keeping the system described into one yml file. Anyway, other solutions are appreciated.

Thanks


Solution

  • Is possible to solve your approach in different ways, and one of them is with docker-compose.

    First, you can start one concrete service (taskX-service) using docker-compose up -d <service_name> You have more details in docker-compose up for only certain containers

    Second, docker-compose also allows you to configure dependencies between containers. If you want to run them in order, you can specify it in depends_on: structure. For example, to execute tasks 1, 2, and 3 in order you could use:

    task1-service: 
      image: task1-image
    task2-service: 
      image: task2-image
      depends_on:
        - task1-service
    task3-service: 
      image: task3-image
      depends_on: 
        - task2-service
    

    Furthermore, this docker-compose.yml is compatible with first I said:

    docker-compose up -d task1-service
    docker-compose up -d task2-service (also launchs task1-service)
    docker-compose up -d task3-service (also launchs task2 and 1 service)
    

    If you don't specify any container, with docker-compose down stops all containers in compose file. I hope it's useful for you.