英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论