-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.go
More file actions
486 lines (428 loc) · 14 KB
/
container.go
File metadata and controls
486 lines (428 loc) · 14 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package container
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/relab/container/build"
"github.com/relab/container/image"
"github.com/relab/container/network"
)
const (
defaultTimeout = 10 * time.Second
)
type Container struct {
client *http.Client
}
// NewContainer creates a new Container instance that can be used to interact with the Docker daemon.
// Currently we support only the default Docker host URL.
func NewContainer() (*Container, error) {
u, err := url.Parse(DefaultDockerHost)
if err != nil {
return nil, err
}
return &Container{
client: &http.Client{
Transport: &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return net.DialTimeout(u.Scheme, u.Path, defaultTimeout)
},
},
},
}, nil
}
// Ping checks if the Docker daemon is reachable and responds to a ping request.
func (c *Container) Ping(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/_ping", nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
return nil
}
// ImagePull requests the docker host to pull an image from a remote registry.
// It executes the privileged function if the operation is unauthorized
// and it tries one more time.
// It's up to the caller to handle the io.ReadCloser and close it properly.
func (c *Container) ImagePull(ctx context.Context, refStr string) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/images/create?fromImage="+refStr, nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
// ImageBuild builds a Docker image from the provided build context.
// The build context should be a tar archive containing the Dockerfile and
// any other files needed for the build.
func (c *Container) ImageBuild(ctx context.Context, buildCtx io.Reader, options build.ImageBuildOptions) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, options.URL(), buildCtx)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-tar")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("image build failed: %s", resp.Status)
}
return resp.Body, nil
}
// ImageRemove removes an image from the docker host.
func (c *Container) ImageRemove(ctx context.Context, imageID string, options image.RemoveOptions) ([]image.DeleteResponse, error) {
imageID = strings.TrimSpace(imageID)
if imageID == "" {
return nil, fmt.Errorf("image ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, options.URL(imageID), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer close(resp)
if resp.StatusCode != http.StatusNoContent {
return nil, fmt.Errorf("image removal failed: %s", resp.Status)
}
var response []image.DeleteResponse
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// NetworkCreate creates a new network in the docker host.
func (c *Container) NetworkCreate(ctx context.Context, options network.CreateOptions) (network.CreateResponse, error) {
body, err := encodeBody(options)
if err != nil {
return network.CreateResponse{}, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/networks/create", body)
if err != nil {
return network.CreateResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return network.CreateResponse{}, err
}
defer close(resp)
if resp.StatusCode != http.StatusCreated {
return network.CreateResponse{}, fmt.Errorf("network creation failed: %s", resp.Status)
}
var response network.CreateResponse
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// NetworkRemove removes an existing network from the docker host.
func (c *Container) NetworkRemove(ctx context.Context, networkID string) error {
networkID = strings.TrimSpace(networkID)
if networkID == "" {
return fmt.Errorf("network ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, "http://localhost/networks/"+networkID, nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("network removal failed: %s", resp.Status)
}
return nil
}
// NetworkConnect connects a container to an existent network in the docker host.
func (c *Container) NetworkConnect(ctx context.Context, networkID, containerID string, config *network.EndpointSettings) error {
networkID = strings.TrimSpace(networkID)
if networkID == "" {
return fmt.Errorf("network ID cannot be empty")
}
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return fmt.Errorf("container ID cannot be empty")
}
body, err := encodeBody(network.ConnectOptions{
Container: containerID,
EndpointConfig: config,
})
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/networks/"+networkID+"/connect", body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("network connect failed: %s", resp.Status)
}
return nil
}
// NetworkDisconnect disconnects a container from an existent network in the docker host.
func (c *Container) NetworkDisconnect(ctx context.Context, networkID, containerID string, force bool) error {
networkID = strings.TrimSpace(networkID)
if networkID == "" {
return fmt.Errorf("network ID cannot be empty")
}
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return fmt.Errorf("container ID cannot be empty")
}
body, err := encodeBody(network.DisconnectOptions{
Container: containerID,
Force: force,
})
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/networks/"+networkID+"/disconnect", body)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("network disconnect failed: %s", resp.Status)
}
return nil
}
// ContainerCreate creates a new container based on the given configuration.
// It can be associated with a name, but it's not mandatory.
func (c *Container) ContainerCreate(ctx context.Context, config *Config, hostConfig *HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (CreateResponse, error) {
body, err := encodeBody(CreateRequest{
Config: config,
HostConfig: hostConfig,
NetworkingConfig: networkingConfig,
})
if err != nil {
return CreateResponse{}, err
}
query := url.Values{}
if containerName != "" {
query.Set("name", containerName)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/containers/create?"+query.Encode(), body)
if err != nil {
return CreateResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return CreateResponse{}, err
}
defer close(resp)
if resp.StatusCode != http.StatusCreated {
return CreateResponse{}, fmt.Errorf("container creation failed: %s", resp.Status)
}
var response CreateResponse
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// ContainerRemove kills and removes a container from the docker host.
func (c *Container) ContainerRemove(ctx context.Context, containerID string, options RemoveOptions) error {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, options.url(containerID), nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("container removal failed: %s", resp.Status)
}
return nil
}
// ContainerStart sends a request to the docker daemon to start a container.
func (c *Container) ContainerStart(ctx context.Context, containerID string) error {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://localhost/containers/"+containerID+"/start", nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("container start failed: %s", resp.Status)
}
return nil
}
// ContainerStop stops a container. In case the container fails to stop
// gracefully within a time frame specified by the timeout argument,
// it is forcefully terminated (killed).
//
// If the timeout is nil, the container's StopTimeout value is used, if set,
// otherwise the engine default. A negative timeout value can be specified,
// meaning no timeout, i.e. no forceful termination is performed.
func (c *Container) ContainerStop(ctx context.Context, containerID string, options StopOptions) error {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, options.url(), nil)
if err != nil {
return err
}
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer close(resp)
return nil
}
const containerWaitErrorMsgLimit = 2 * 1024 // 2KiB
// ContainerWait waits until the specified container is in a certain state
// indicated by the given condition, either "not-running" (default),
// "next-exit", or "removed".
//
// This is a blocking call that will return when the container is in the specified state.
//
// This is different from the original Docker API, which returns a channel
// that can be used to wait for the container state change.
// See the original [ContainerWait] API documentation for more details.
//
// [ContainerWait]: https://pkg.go.dev/github.com/docker/docker/client#Client.ContainerWait
func (c *Container) ContainerWait(ctx context.Context, containerID string, condition WaitCondition) (WaitResponse, error) {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return WaitResponse{}, fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, condition.url(containerID), nil)
if err != nil {
return WaitResponse{}, err
}
resp, err := c.client.Do(req)
if err != nil {
return WaitResponse{}, err
}
defer close(resp)
var buf bytes.Buffer
stream := io.TeeReader(resp.Body, &buf)
var result WaitResponse
if err := json.NewDecoder(stream).Decode(&result); err != nil {
// Try to extract plaintext error message (e.g. from proxy)
if errors.As(err, new(*json.SyntaxError)) {
body, _ := io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
return WaitResponse{}, fmt.Errorf("malformed response: %s%s", buf.String(), string(body))
}
return WaitResponse{}, err
}
return result, nil
}
// ContainerInspect returns the container information.
func (c *Container) ContainerInspect(ctx context.Context, containerID string) (InspectResponse, error) {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return InspectResponse{}, fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/containers/"+containerID+"/json", nil)
if err != nil {
return InspectResponse{}, err
}
resp, err := c.client.Do(req)
if err != nil {
return InspectResponse{}, err
}
defer close(resp)
if resp.StatusCode != http.StatusOK {
return InspectResponse{}, fmt.Errorf("container inspect failed: %s", resp.Status)
}
var response InspectResponse
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
}
// ContainerLogs returns the logs generated by a container in an io.ReadCloser.
// It's up to the caller to close the stream.
//
// The stream format on the response will be in one of two formats:
//
// If the container is using a TTY, there is only a single stream (stdout), and
// data is copied directly from the container output stream, no extra
// multiplexing or headers.
//
// If the container is *not* using a TTY, streams for stdout and stderr are
// multiplexed.
// The format of the multiplexed stream is as follows:
//
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
//
// STREAM_TYPE can be 1 for stdout and 2 for stderr
//
// SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
// This is the size of OUTPUT.
//
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
// stream.
func (c *Container) ContainerLogs(ctx context.Context, containerID string, options LogsOptions) (io.ReadCloser, error) {
containerID = strings.TrimSpace(containerID)
if containerID == "" {
return nil, fmt.Errorf("container ID cannot be empty")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, options.url(containerID), nil)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func encodeBody(obj any) (*bytes.Buffer, error) {
if obj == nil {
return nil, nil
}
body := bytes.NewBuffer(nil)
if err := json.NewEncoder(body).Encode(obj); err != nil {
return nil, err
}
return body, nil
}
func close(resp *http.Response) {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}