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

Issue

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.

Solution

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.

Answered By – Aron

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.