-
Notifications
You must be signed in to change notification settings - Fork 3
/
container.go
53 lines (46 loc) · 1.24 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
package main
import (
"fmt"
"os"
"strings"
"time"
docker "github.com/fsouza/go-dockerclient"
)
func CreateContainer(client *docker.Client, config *Config, image string, standby int, env string) (*docker.Container, error) {
id, _ := randomHex(20)
volumePath := fmt.Sprintf("%s/%s", config.SharedPath, id)
name := fmt.Sprintf("bitrun-%v", time.Now().UnixNano())
if err := os.Mkdir(volumePath, 0777); err != nil {
return nil, err
}
opts := docker.CreateContainerOptions{
Name: name,
HostConfig: &docker.HostConfig{
Binds: []string{
volumePath + ":/code",
volumePath + ":/tmp",
},
ReadonlyRootfs: true,
Memory: config.MemoryLimit,
MemorySwap: 0,
},
Config: &docker.Config{
Hostname: "bitrun",
Image: image,
Labels: map[string]string{"id": id},
AttachStdout: false,
AttachStderr: false,
AttachStdin: false,
Tty: false,
NetworkDisabled: config.NetworkDisabled,
WorkingDir: "/code",
Cmd: []string{"sleep", fmt.Sprintf("%v", standby)},
Env: strings.Split(env, "\n"),
},
}
container, err := client.CreateContainer(opts)
if err == nil {
container.Config = opts.Config
}
return container, err
}