/
docker.go
148 lines (126 loc) · 2.73 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
141
142
143
144
145
146
147
148
package executors
import (
"context"
"fmt"
"io"
"os"
"os/user"
"runtime"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
cp "github.com/otiai10/copy"
)
func DockerRunImage(
ctx context.Context,
cli *client.Client,
image string,
pull bool,
privileged bool,
src string,
dst string,
onID func(id string),
stdout io.Writer,
env map[string]string,
renderTemplates func(workdir string, ejecting bool) error,
cmds []string,
) error {
currentUser, err := user.Current()
if err != nil {
return err
}
if runtime.GOOS != "windows" {
env["DST_UID"] = currentUser.Uid
env["DST_GID"] = currentUser.Gid
}
images, err := cli.ImageList(ctx, types.ImageListOptions{})
if err != nil {
return err
}
imageExists := false
o:
for _, i := range images {
for _, t := range i.RepoTags {
if t == image {
imageExists = true
break o
}
}
}
if pull || !imageExists {
reader, err := cli.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
return err
}
if _, err := io.Copy(stdout, reader); err != nil {
return err
}
}
workdir, err := os.MkdirTemp("", "hydrapp-build-dir-")
if err != nil {
return err
}
defer os.RemoveAll(workdir)
if err := cp.Copy(src, workdir); err != nil {
return err
}
if err := renderTemplates(workdir, false); err != nil {
return err
}
var cmd []string
binds := []string{
workdir + ":/work:z",
}
if len(cmds) > 0 {
cmd = cmds
} else {
binds = append(binds, dst+":/dst:z")
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: image,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
OpenStdin: true,
Tty: true,
Env: func() []string {
out := []string{}
for key, value := range env {
out = append(out, key+"="+value)
}
return out
}(),
Cmd: cmd,
}, &container.HostConfig{
Privileged: privileged,
Binds: binds,
}, nil, nil, "")
if err != nil {
return err
}
onID(resp.ID)
waiter, err := cli.ContainerAttach(ctx, resp.ID, container.AttachOptions{
Stdin: false,
Stdout: true,
Stderr: true,
Stream: true,
})
if err != nil {
return err
}
defer waiter.Close()
go io.Copy(stdout, waiter.Reader) // We intentionally ignore errors here since we can't handle them
if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return err
}
statusChan, errChan := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errChan:
return err
case status := <-statusChan:
if (status.StatusCode != 0 && status.StatusCode != 137) || status.Error != nil {
return fmt.Errorf("could not wait for container: %v", status)
}
}
return nil
}