Docker Community Forums

Share and learn in the Docker community.

How to use compose-cli API

Hi,

I’m trying to use the compose-cli in a CLI I’m building to remove the docker-compose dependence.

Currently we have something like-

exec.Command("docker-compose", "-f", utils.DockerComposeFile, "up", "-d")

But we want to use the compose-cli API to run docker-compose commands.

I found this function in compose-cli/api/compose/api.go-

// Up executes the equivalent to a `compose up`
Up(ctx context.Context, project *types.Project, options UpOptions) error

This is exactly what we want but I could not find any implementation of this or any docs for this. I’m not sure how to pass in the project argument and all.

Could someone please guide me? Sorry if this is a noob question…

Thank you!

Hi @navendup,
You could use Compose-CLI API indeed, but the API here are subject to change, so you expose yourself to the need to update your code when switching to newer versions.
In order to load the project struct, you can check how the Up() command is used and you will do something like project, err := opts.toProject(services) here. However, you’ll see in the code the project struct might be amended in several places, so it’s not likely you will manage all possible cases with a one-liner to initialise it.
The safer solution to replace your exec.Command("docker-compose", "-f", utils.DockerComposeFile, "up", "-d") today is probably to just remove the space and instead use exec.Command("docker compose", "-f", utils.DockerComposeFile, "up", "-d"). Maybe not as nice as you still rely on shellout, but you’ll be safer in terms of compatibility with further releases and avoid re-coding things for various edge cases.
Hope this helps

Thank you for the reply. Let me see if I will be able to implement this using the API.

We just have some pretty straightforward use cases currently, not sure how this will work out. If it is hard to maintain, we might stick with the shell command