/
container.go
48 lines (39 loc) · 1.24 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package dockerClient
import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
)
type DockerContainer interface {
RunContainer(config *container.Config, hostConfig *container.HostConfig) (container.ContainerCreateCreatedBody, error)
ContainerExec(id string, command []string) (resp types.ContainerExecInspect, err error)
}
func (c *CLI) RunContainer(config *container.Config, hostConfig *container.HostConfig) (container container.ContainerCreateCreatedBody, err error) {
container, err = c.Client.ContainerCreate(c.Ctx, config, hostConfig, nil, "")
if err != nil {
return container, err
}
err = c.Client.ContainerStart(c.Ctx, container.ID, types.ContainerStartOptions{})
return container, err
}
func (c *CLI) ContainerExec(id string, command []string) (resp types.ContainerExecInspect, err error) {
out2, err := c.Client.ContainerExecCreate(c.Ctx, id, types.ExecConfig{
Cmd: command,
})
if err != nil {
return resp, err
}
err = c.Client.ContainerExecStart(c.Ctx, out2.ID, types.ExecStartCheck{})
if err != nil {
return resp, err
}
for {
resp, err = c.Client.ContainerExecInspect(c.Ctx, out2.ID)
if err != nil {
return resp, err
}
if !resp.Running {
break
}
}
return resp, err
}