docker - golang如何获取容器ID
问题描述
我使用 golang 开发应用程序。我想在应用程序中获取容器。我已经厌倦了 shell。但我想通过 go 获取容器。谢谢
解决方案
您可以使用 docker/client
https://godoc.org/github.com/docker/docker/client
示例代码:
# listcontainers.go
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
}
}
然后像这样执行
DOCKER_API_VERSION=1.35 go run listcontainers.go
有关 docker 引擎 SDK 和 API 的更多信息
https://docs.docker.com/develop/sdk/
推荐阅读
- python - 如何解释 scipy.interpolate.splrep 的结果?
- r - 匹配R中的两个整数向量
- django-logging - Django 日志记录:django.db.backends 返回“(0.000) None; args=None”消息
- docker - .NET Core 2.2 docker core dumped 故障排除
- python - Wxpython - 如何扩展 StaticBoxSizer?
- amazon-web-services - 我可以只允许单个 API 密钥访问单个 AWS API Gateway 终端节点吗?
- excel - Excel需要更多时间打开> 10k条记录:apache poi
- java - 为什么draw()方法中的repaint()不起作用(不调用paint())?
- android - 如果代码中存在 java.util.Calendar.getInstance(),则应用程序在即时运行后崩溃
- python - 无法在 R 中运行 python 脚本(sys$stdout$flush() 中的错误:尝试应用非函数)