-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.go
More file actions
67 lines (60 loc) · 1.53 KB
/
run.go
File metadata and controls
67 lines (60 loc) · 1.53 KB
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
package dockercommand
import (
docker "github.com/fsouza/go-dockerclient"
)
type RunOptions struct {
Name string
Image string
Cmd []string
VolumeBinds []string
Links []string
PublishAllPorts bool
PortBindings map[docker.Port][]docker.PortBinding
Detach bool
Env map[string]string
LoggingDriver string
LoggingDriverConfig map[string]string
NetworkMode string
}
func (dock *Docker) Run(options *RunOptions) (*Container, error) {
if err := dock.pullImageIfNotExist(options.Image); err != nil {
return nil, err
}
container, err := dock.client.CreateContainer(docker.CreateContainerOptions{
Name: options.Name,
Config: &docker.Config{
Image: options.Image,
Cmd: options.Cmd,
Env: convertEnvMapToSlice(options.Env),
},
})
if err != nil {
return nil, err
}
err = dock.Start(&StartOptions{
ID: container.ID,
VolumeBinds: options.VolumeBinds,
Links: options.Links,
PublishAllPorts: options.PublishAllPorts,
PortBindings: options.PortBindings,
NetworkMode: options.NetworkMode,
LogConfig: docker.LogConfig{
Type: options.LoggingDriver,
Config: options.LoggingDriverConfig,
},
})
if err != nil {
return nil, err
}
containerWrapper := &Container{
info: container,
client: dock.client,
}
if !options.Detach {
_, err = dock.client.WaitContainer(container.ID)
if err != nil {
return nil, err
}
}
return containerWrapper, nil
}