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

huangapple go评论78阅读模式
英文:

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

问题

我想使用Golang的docker包通过提供docker-compose.yml文件来运行docker-compose项目。

根据https://docs.docker.com/engine/api/sdk/examples/的示例,我知道如何使用Golang创建和运行单个容器,但是否有办法使用Golang的docker库运行docker-compose项目?

我知道可以像这样做:

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

但我希望使用docker包来完成这个操作。

英文:

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.

答案1

得分: 3

我是一个完全的Go新手,所以对我的观点持保留态度,但我正在查看Docker Compose的实现,他们使用Cobra作为他们的命令行工具(这似乎很常见),以及Viper用于参数解析。提供的链接是Cobra命令的实现,与命令使用的内部实现是不同的。

如果你想要添加一个命令,作为你的Go命令的一部分来调用Docker Compose的"up"命令(这是我认为你想要做的 - 这也是我试图尝试做的),我认为你必须接受这样一个事实,即你必须基本上实现自己的版本的Cobra命令,复制他们现有的逻辑。这是我的理解。

英文:

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.

huangapple
  • 本文由 发表于 2021年7月26日 11:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/68524337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定