docker_run.go
96 lines1
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
package platform
import "fmt"
// Start runs a service container on the server.
func (s *Server) Start(service *Service) error {
args := []string{"docker", "run", "-d", "--name", service.Name}
// Add restart policy
if service.Restart != "" {
args = append(args, "--restart", service.Restart)
}
// Add network
if service.Network != "" {
args = append(args, "--network", service.Network)
}
// Add ports
for _, p := range service.Ports {
portMap := fmt.Sprintf("%d:%d", p.Host, p.Container)
if p.Bind != "" {
portMap = p.Bind + ":" + portMap
}
args = append(args, "-p", portMap)
}
// Add volumes
for _, v := range service.Volumes {
args = append(args, "-v", v.Source+":"+v.Target)
}
// Add environment variables
for k, v := range service.Env {
args = append(args, "-e", k+"="+v)
}
// Add healthcheck
if h := service.Healthcheck; h != nil {
if h.Cmd != "" {
args = append(args, "--health-cmd", h.Cmd)
}
if h.Interval != "" {
args = append(args, "--health-interval", h.Interval)
}
if h.Timeout != "" {
args = append(args, "--health-timeout", h.Timeout)
}
if h.Retries > 0 {
args = append(args, "--health-retries", fmt.Sprintf("%d", h.Retries))
}
if h.StartPeriod != "" {
args = append(args, "--health-start-period", h.StartPeriod)
}
}
// Add resource limits
if r := service.Resources; r != nil {
if r.CPUs != "" {
args = append(args, "--cpus", r.CPUs)
}
if r.Memory != "" {
args = append(args, "--memory", r.Memory)
}
}
// Add logging configuration
if l := service.Logging; l != nil {
if l.Driver != "" {
args = append(args, "--log-driver", l.Driver)
}
for k, v := range l.Options {
args = append(args, "--log-opt", k+"="+v)
}
}
// Add privileged flag
if service.Privileged {
args = append(args, "--privileged")
}
// Add image
args = append(args, service.Image)
// Add command
args = append(args, service.Command...)
_, err := s.SSH(args...)
return err
}
// Stop kills and removes a container on the server.
func (s *Server) Stop(name string) error {
_, err := s.SSH("docker", "rm", "-f", name)
return err
}