代码拉取完成,页面将自动刷新
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")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。