Docker compose > service > conditional feature

Hi,

I’m interested having some kind of conditional statement for my docker-compose file.
(sorry if this topic already exists somewhere, just give me the link)

My use case:

  • my backend compose file includes middlewares required for integration tests (ex. database),
  • plus a version of a portal.
  • while working locally, I do need the portal to do some manual tests,
  • but while testing/releasing under jenkins, I only need middlewares, and so I don’t want portal service to be pulled/started at all.
  • I would like to compose something like the following :

Example:

mdw1:
  image: mdwr1
mdw2:
  image: mdwr2
portal:
  image: myportal
  rule: if: '$WITH_PORTAL= "1"'

As I understood from StackOverflow (BMich answer) , there is nothing to do that today out of managing multiple docker-compose files.

After seeing issue on github I found this closest question saying that conditional is out-of-scope.

My point is: I would ike to avoid to duplicate the information (eg. mdw part), and avoid managing X docker-compose files to fit my need with extra scripts…so for me conditional statement represent a nice-to-have feature, and I would like to have more details on why it’s rejected.

For gitlab-ci yml, you have “rule” directive that could match that need.

If you think this could be a feature request on github, we could (I could) create a github feature request ?


PS as new user here I can’t post more than 1 link : and there is an issue with the popup saying that > then I were unable to re-edit the content to fix it :frowning: I would prefer captcha to protect against new user spamming risk…


So reference used are:

  • StackOverflow subject: search “docker-compose-conditional-statement-e-g-add-volume-only-if-condition”
  • gitlab ci rule : search via docs_gitlab_com then ce/ci/yaml/#rules

There’s tons of way to generates files be it a script or an ansible template.
Compose files scope is to describe what docker have to create/start. Use your favorite automating solution to templates your compose.

Basically what you want is helm charts for docker. In these case I use ansible as a simple remplacement

Those two template engines cought my attention recently: https://gomplate.ca/ and https://get-ytt.io/.

You can pipe the rendered output and deploy on the fly using docker stack deploy -c - {stackname} and docker-compose -f - ...}.

This is absurd. In what world is writing scripts to write scripts and then managing that catastrophe better than introducing a trivial and common feature like a conditional check? What a lazy response.

1 Like