/
client.go
60 lines (54 loc) · 2.84 KB
/
client.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
49
50
51
52
53
54
55
56
57
58
59
60
package libdocker
import (
"log"
"time"
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
//dockerimagetypes "github.com/docker/docker/api/types/image"
//dockernetworktypes "github.com/docker/docker/api/types/network"
dockerapi "github.com/docker/docker/client"
)
type Interface interface {
ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error)
InspectContainer(id string) (*dockertypes.ContainerJSON, error)
CreateContainer(dockertypes.ContainerCreateConfig) (*dockercontainer.ContainerCreateCreatedBody, error)
StartContainer(id string) error
RestartContainer(id string, timeout time.Duration) error
StopContainer(id string, timeout time.Duration) error
WaitContainer(name string, timeout time.Duration) error
UpdateContainerResources(id string, updateConfig dockercontainer.UpdateConfig) error
RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error
InspectImageByRef(imageRef string) (*dockertypes.ImageInspect, error)
InspectImageByID(imageID string) (*dockertypes.ImageInspect, error)
ListImages(opts dockertypes.ImageListOptions) ([]dockertypes.ImageSummary, error)
PullImage(image string, auth dockertypes.AuthConfig, opts dockertypes.ImagePullOptions) error
RemoveImage(image string, opts dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error)
Logs(string, dockertypes.ContainerLogsOptions, StreamOptions) error
Version() (*dockertypes.Version, error)
Info() (*dockertypes.Info, error)
AttachExec(string, dockertypes.ExecStartCheck) (*dockertypes.HijackedResponse, error)
CreateExec(string, dockertypes.ExecConfig) (*dockertypes.IDResponse, error)
StartExec(string, dockertypes.ExecStartCheck, StreamOptions) error
InspectExec(id string) (*dockertypes.ContainerExecInspect, error)
InspectNetwork(id string) (dockertypes.NetworkResource, error)
CreateNetwork(id string) (dockertypes.NetworkCreateResponse, error)
ConnectContainerToNetwork(id string, containerid string) error
DisconnectContainerFromNetwork(id string, containerid string, force bool) error
RemoveNetwork(id string) error
ServerVersion() (dockertypes.Version, error)
}
func getDockerClient(dockerEndpoint string) (*dockerapi.Client, error) {
if len(dockerEndpoint) > 0 {
log.Printf("Connection to docker on %s", dockerEndpoint)
log.Println()
return dockerapi.NewClient(dockerEndpoint, "", nil, nil)
}
return dockerapi.NewClientWithOpts(dockerapi.FromEnv, dockerapi.WithAPIVersionNegotiation())
}
func ConnectToDockerOrDie(requestTimeout, imagePullProgressDeadline time.Duration) Interface {
client, err := dockerapi.NewClientWithOpts(dockerapi.FromEnv, dockerapi.WithAPIVersionNegotiation())
if err != nil {
log.Fatalf("Couldn't connect to docker: %v", err)
}
return newSkupDockerClient(client, requestTimeout, imagePullProgressDeadline)
}