-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
203 lines (160 loc) · 4.48 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gopker
import (
"docker.io/go-docker"
dockerMount "docker.io/go-docker/api/types/mount"
"docker.io/go-docker/api/types"
dContainer "docker.io/go-docker/api/types/container"
"github.com/docker/go-connections/nat"
"golang.org/x/net/context"
"io"
"log"
"os"
)
type containerStatus int
const (
READY containerStatus = 0
RUNNING containerStatus = 1
STOPPED containerStatus = 2
DEAD containerStatus = 3
)
type dockerContext struct {
DockerApiClient *docker.Client
DockerContext *context.Context
}
type portMapping struct {
hostPort string
containerPort string
protocol string
}
type mount struct {
target string
source string
}
type container struct {
containerID string
image string
status containerStatus
ipAddress string
portMappings []portMapping
mounts []mount
environments []string
context *dockerContext
}
type Container interface {
Start() (types.ContainerJSON, error)
Stop() error
Kill() error
PortMapping(string, string, ...string) Container
Env(string) Container
Mount(string, string) Container
}
func NewContainer(image string) (Container, error) {
ctx := context.Background()
cli, err := docker.NewEnvClient()
if err != nil {
return nil, err
}
return &container{
image: image,
status: READY,
portMappings: make([]portMapping, 0),
mounts: make([]mount, 0),
context: &dockerContext{
DockerApiClient: cli,
DockerContext: &ctx,
}}, nil
}
func (container *container) PortMapping(hostPort string, containerPort string, protocols ...string) Container {
protocol := "tcp"
if len(protocols) > 0 {
protocol = protocols[0]
} else if len(protocols) > 1 {
protocol = protocols[0]
log.Println("Only first protocol is used.")
}
container.portMappings = append(
container.portMappings,
portMapping{hostPort, containerPort, protocol},
)
return container
}
func (container *container) Mount(target string, source string) Container {
container.mounts = append(container.mounts, mount {
target: target,
source: source})
return container
}
func (container *container) Env(env string) Container {
container.environments = append(container.environments, env)
return container
}
func (container *container) Start() (types.ContainerJSON, error) {
ctx := container.context.DockerContext
cli := container.context.DockerApiClient
if r, err := cli.ImagePull(*ctx, container.image, types.ImagePullOptions{}); err != nil {
return types.ContainerJSON{}, err
} else {
io.Copy(os.Stdout, r)
}
resp, err := cli.ContainerCreate(*ctx, &dContainer.Config{
Image: container.image,
Env: container.environments,
Tty: true,
}, &dContainer.HostConfig{
Mounts: prepareMounts(container.mounts),
PortBindings: prepareBindings(container.portMappings),
}, nil, "")
if err != nil {
return types.ContainerJSON{}, err
}
container.containerID = resp.ID
if err := cli.ContainerStart(*ctx, container.containerID, types.ContainerStartOptions{}); err != nil {
return types.ContainerJSON{}, err
}
container.status = RUNNING
inspectionResult, err := cli.ContainerInspect(*ctx, container.containerID)
if err != nil {
return types.ContainerJSON{}, err
}
container.ipAddress = inspectionResult.NetworkSettings.IPAddress
return inspectionResult, nil
}
func (container *container) Stop() error {
ctx := container.context.DockerContext
cli := container.context.DockerApiClient
if err := cli.ContainerStop(*ctx, container.containerID, nil); err != nil {
return err
}
container.status = STOPPED
return nil
}
func (container *container) Kill() error {
ctx := container.context.DockerContext
cli := container.context.DockerApiClient
if err := cli.ContainerKill(*ctx, container.containerID, "9"); err != nil {
return err
}
container.status = DEAD
return nil
}
func prepareMounts(mountMappings []mount) []dockerMount.Mount {
mountMap := make([]dockerMount.Mount, 0)
for _, mountMapping := range mountMappings {
mountMap = append(mountMap, dockerMount.Mount {
Type: "bind",
Source: mountMapping.source,
Target: mountMapping.target,
})
}
return mountMap
}
func prepareBindings(portMappings []portMapping) nat.PortMap {
portMap := make(nat.PortMap)
for _, portMapping := range portMappings {
portBindings := make([]nat.PortBinding, 0)
portBindings = append(portBindings, nat.PortBinding{"0.0.0.0", portMapping.hostPort})
containerPort := nat.Port(portMapping.containerPort + "/" + portMapping.protocol)
portMap[containerPort] = portBindings
}
return portMap
}