From Golang, how to build/run a docker-compose project?
Asked Answered
H

1

7

I would like to run docker-compose projects from Golang using the docker package by providing the docker-compose.yml file.

Following the example from https://docs.docker.com/engine/api/sdk/examples/ I know how to create and run individual containers using Golang, but is there a way to run docker-compose projects from the Golang docker library?

I know that I can do something like this

import "os/exec"
exec.Command("docker-compose","up")

but I would like this to happen from the docker package instead.

Hamer answered 26/7, 2021 at 3:56 Comment(2)
check compose-go or libcomposeBritanybritches
Thanks @Chandan, libcompose seems to not be maintained anymore but compose-go looks promising. Unfortunately I cannot find any examples of how to use compose-go, would you be able to provide a "getting started" example?Hamer
C
3

I'm a complete Go noob, so take this with a grain of salt, but I was looking through the Docker compose implementation of their up command - they use Cobra for their cli tooling (which seems quite common), and Viper for argument parsing. The link provided is to the Cobra command implementation, which is different from the up internals, which the command uses.

If you were to want to add a command which would invoke the docker compose "up" command as part of your golang command (which is what I think you're going for - it's what I was looking to try to do), I think you'd have to accept the fact that you'd have to basically implement your own versions of the Cobra commands they have there, replicating their existing logic. That was my take.

Cody answered 27/2, 2022 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.