1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
docker.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
package docker
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
// Client for Docker
type Client struct {
cli *client.Client
}
// NewClient builds and returns a docker Client
func NewClient() (Client, error) {
c, err := client.NewEnvClient()
return Client{cli: c}, err
}
// ContainerStart pulls and starts the given container
func (c Client) ContainerStart(image string, cmd ...string) (string, error) {
ctx := context.Background()
if _, err := c.cli.ImagePull(ctx, image, types.ImagePullOptions{}); err != nil {
return "", err
}
resp, err := c.cli.ContainerCreate(ctx, &container.Config{
Image: image,
Cmd: cmd,
}, nil, nil, "")
if err != nil {
return "", err
}
if err := c.cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return "", err
}
return resp.ID, nil
}
// ContainerWait waits for a container to finish
func (c Client) ContainerWait(ID string) error {
ctx := context.Background()
_, err := c.cli.ContainerWait(ctx, ID)
return err
}
// ContainerKill kills the given container
func (c Client) ContainerKill(ID string) error {
ctx := context.Background()
return c.cli.ContainerKill(ctx, ID, "KILL")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v6.1.4

搜索帮助