This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
/
create_container.go
89 lines (77 loc) · 1.88 KB
/
create_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
package docker
import (
"context"
"fmt"
"os/exec"
goruntime "runtime"
"strings"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/frantjc/sequence/runtime"
)
func (r *dockerRuntime) CreateContainer(ctx context.Context, s *runtime.Spec) (runtime.Container, error) {
pref, err := reference.Parse(s.Image)
if err != nil {
return nil, err
}
var (
addr = r.client.DaemonHost()
conf = &container.Config{
Image: pref.String(),
Entrypoint: s.Entrypoint,
Cmd: s.Cmd,
WorkingDir: s.Cwd,
Env: append(s.Env, fmt.Sprintf("DOCKER_HOST=%s", addr)),
Labels: labels,
}
hconf = &container.HostConfig{
Privileged: s.Privileged,
}
)
if strings.HasPrefix(addr, "unix://") {
sock := strings.TrimPrefix(addr, "unix://")
hconf.Mounts = append(hconf.Mounts, mount.Mount{
Source: sock,
Target: "/var/run/docker.sock",
Type: runtime.MountTypeBind,
})
}
if goruntime.GOOS == "linux" {
if docker, err := exec.LookPath("docker"); err == nil {
hconf.Mounts = append(hconf.Mounts, mount.Mount{
Source: docker,
Target: "/usr/local/bin/docker",
Type: runtime.MountTypeBind,
})
}
}
for _, m := range s.Mounts {
dt := mount.Type(m.Type)
dm := mount.Mount{
Type: dt,
Source: m.Source,
Target: m.Destination,
}
switch dt {
case mount.TypeBind:
case mount.TypeVolume:
dm.VolumeOptions = &mount.VolumeOptions{
Labels: labels,
}
case mount.TypeTmpfs:
dm.Source = ""
}
for _, opt := range m.Options {
if opt == runtime.MountOptReadOnly {
dm.ReadOnly = true
}
}
hconf.Mounts = append(hconf.Mounts, dm)
}
createResp, err := r.client.ContainerCreate(ctx, conf, hconf, nil, nil, "")
if err != nil {
return nil, err
}
return &dockerContainer{id: createResp.ID, client: r.client}, nil
}