-
Notifications
You must be signed in to change notification settings - Fork 62
/
docker.go
140 lines (121 loc) · 3.45 KB
/
docker.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
// startContainer starts a docker container and returns the container ID
// as well as a websocket connection to the attach endpoint.
func (h *handler) startContainer() (string, *websocket.Conn, error) {
securityOpts := []string{
"no-new-privileges",
}
b := bytes.NewBuffer(nil)
if err := json.Compact(b, []byte(seccompProfile)); err != nil {
return "", nil, fmt.Errorf("compacting json for seccomp profile failed: %v", err)
}
securityOpts = append(securityOpts, fmt.Sprintf("seccomp=%s", b.Bytes()))
dropCaps := &strslice.StrSlice{"NET_RAW"}
// create the container
r, err := h.dcli.ContainerCreate(
context.Background(),
&container.Config{
Image: defaultDockerImage,
Cmd: []string{"sh"},
Tty: true,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
OpenStdin: true,
StdinOnce: true,
},
&container.HostConfig{
SecurityOpt: securityOpts,
CapDrop: *dropCaps,
NetworkMode: "none",
LogConfig: container.LogConfig{
Type: "none",
},
Resources: container.Resources{
PidsLimit: 5,
},
},
nil, "")
if err != nil {
return "", nil, err
}
// connect to the attach websocket endpoint
header := http.Header(make(map[string][]string))
header.Add("Origin", h.dockerURL.String())
v := url.Values{
"stdin": []string{"1"},
"stdout": []string{"1"},
"stderr": []string{"1"},
"stream": []string{"1"},
}
wsURL := fmt.Sprintf("wss://%s/%s/containers/%s/attach/ws?%s", h.dockerURL.Host, dockerAPIVersion, r.ID, v.Encode())
var dialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: h.tlsConfig,
}
conn, _, err := dialer.Dial(wsURL, header)
if err != nil {
return r.ID, nil, fmt.Errorf("dialing %s with header %#v failed: %v", wsURL, header, err)
}
// start the container
if err := h.dcli.ContainerStart(context.Background(), r.ID, types.ContainerStartOptions{}); err != nil {
return r.ID, conn, err
}
return r.ID, conn, nil
}
// removeContainer removes with force a container by it's container ID.
func (h *handler) removeContainer(cid string) error {
if err := h.dcli.ContainerRemove(context.Background(), cid,
types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}); err != nil {
return err
}
logrus.Debugf("removed container: %s", cid)
return nil
}
// pullImage requests a docker image if it doesn't exist already.
func (h *handler) pullImage(image string) error {
exists, err := h.imageExists(image)
if err != nil {
return err
}
if exists {
return nil
}
resp, err := h.dcli.ImagePull(context.Background(), image, types.ImagePullOptions{})
if err != nil {
return err
}
fd, isTerm := term.GetFdInfo(os.Stdout)
return jsonmessage.DisplayJSONMessagesStream(resp, os.Stdout, fd, isTerm, nil)
}
// imageExists checks if a docker image exists.
func (h *handler) imageExists(image string) (bool, error) {
_, _, err := h.dcli.ImageInspectWithRaw(context.Background(), image)
if err == nil {
return true, nil
}
if client.IsErrNotFound(err) {
return false, nil
}
return false, err
}