在Go中调用Docker-Compose命令

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

Call Docker-Compose commands in Go

问题

我想用Go语言编写一个CLI,为Docker-Compose提供一些便利功能,但不幸的是,我找不到与API相关的文档。我在代码库中进行了搜索,但代码结构相当复杂,我无法深入了解。目前,我唯一的选择似乎是使用shell执行命令。

我目前最好的猜测是直接调用(*github.com/docker/compose/pkg/api.ServiceProxy).RunOneOffContainer,但我似乎无法弄清楚如何获取所有依赖项,如cli和context。

简而言之,我如何以编程方式调用docker-compose的命令,比如up

英文:

I want to make a CLI that provides some QoL Features for Docker-Compose in go, but unfortunately, I can't find any docs related to the API. I searched through the repo, but the codebase is quite nested and I can't get to the bottom of it. My only choice atm seems to just execute the commands using the shell..

My current best guess is to directly invoke (*github.com/docker/compose/pkg/api.ServiceProxy).RunOneOffContainer but I can't seem to figure out how to get all the dependencies like the cli and context.

TL;DR: how do I programmatically call docker-compose commands like up

答案1

得分: 1

没有docker compose API。如果你选择使用shell执行docker-compose命令,那么在Go中使用'exec.Command':

out, err := exec.Command("docker", "compose", "up").Output()
if err != nil {
    log.Fatal(err)
}
英文:

There is no docker compose API. If you choice execute the docker-compose commands using the shell, then use 'exec.Command' in Go:

out, err := exec.Command("docker", "compose", "up").Output()
if err != nil {
	log.Fatal(err)
}

huangapple
  • 本文由 发表于 2022年5月9日 20:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72171763.html
匿名

发表评论

匿名网友

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

确定