Optional depends_on depending on profile

Hello

I’m running a set of containers as defined as follows by a docker-compose file:

services:
  experience:
    image: experience
    profile:
      - measure1
      - measure2

  measure1:
    image: measure1
    profile:
      - measure1
    depends_on:
      experience:
        condition: service_healthy
  
  measure2:
    image: measure2
    profile:
      - measure2
    depends_on:
      experience:
        condition: service_healthy
  
  analyze:
    image: analyze
    depends_on:
      measure1:
        condition: service_completed_successfully
      measure2:
        condition: service_completed_successfully

I would want the depends_on of analyze to not be an issue when running only one of the measure containers.
This comes from the fact that measuring can be really long and I may want to change only one of the measurement protocols or the analysis and use the data of a former measurement.

Currently, I’m using environment variables for the measure containers to run but exit as soon as they are not needed, but I recently discovered the profiles and they seem made for that purpose.

I’m deploying everything using docker-compose v3.9 and running on ArchLinux with a docker server and client 24.0.2 and an API 1.43.

Thank you in advance and I stay at your disposal for further details

Profiles can be used to run specific services based on the chosen profiles, but in your case I’m not even sure that Docker Compose is what you need. By the way there is no Docker Compoe v3.9. You probably mean that you are using the compose yaml syntax version 3.9 which is ignored when you are using Docker Compose v2 where v2 is actually the version of Docker Compose and can be installed as a docker cli plugin. Docker Compose v1 is not developed anymore.

Back to the question. If you use containers for measurements and handle the result in another container, I think you could just write a simple shell script to run the containers one by one and at the and the “analyze” container. OR you can have one compose file for the measurements and another to analyze. Since I also prefer Docker Compose, I would recomend two compose files. “measure.yml” and “analyze.yml” and define the compose file path (-f analyze.yml) when you run docker compose. You can use that with profiles in measure.yml.

You could also use multiple compose files and merge them like this depending on which yaml you need:

docker compose -f measure.yml -f analyze-require-measure1.yml up -d

That way analyze-require-measure1.yml could depend on measure1 and automatically start the required services without profiles.

1 Like