Home > front end >  How can you conditionally set services in the docker-compose.yml file?
How can you conditionally set services in the docker-compose.yml file?

Time:11-17

I'm new to using Docker and Cake. At the moment we have a simple Cake task that runs the DockerComposeUp() method that takes a DockerComposeUpSettings object. The docker-compose.yaml file holds some info on a service that I want to conditionally run (serviceA):

version: "1.0"  
services:  
    serviceA:  
        image: someImage  
        ports:
          -"000000"
          -"000001"
    serviceB:
        image: someOtherImage
        anotherProperty: somethingElse
        ports:
          -"111111"

I've tried splitting out serviceA into a separate docker-compose file called 'docker-compose.serviceA.yaml' and calling it by adding to the DockerComposeUpSettings.ArgumentCustomization the following:

if(some setting)
{
    dockerComposeUpSettings.ArgumentCustomization = builder => builder.Append("-f docker-compose.yaml -f docker-compose.serviceA.yaml");
}

However, Cake throws the following error:

"unknown shorthand flag: 'f' in -f"

How can I merge to docker-compose files as part of the DockerComposeUp method using Cake?

Update

I've found there is a 'Files' property on the DockerComposeUpSettings object (inherited from DockerComposeSettings object), where you can declare the configuration files. So I've added:

if(some flag)
{
    dockerComposeSettings.Files = new[]{ "docker-compose.yaml", "docker-compose.serviceA.yaml" };
}

CodePudding user response:

I don't know much about docker, but looking at the docs here and here it seems it would be important to have the -f option set before the command specified on the commandline. Your customization (builder.Append()) puts them at the end of the commandline.

Have you tried setting the Files property of the DockerComposeUpSettings? That looks like what you are looking for.

  • Related