-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker.go
72 lines (58 loc) · 1.67 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
/*
docker.go contains all of the logic specific to executing docker processes
from inside the application
*/
package main
import (
"os/exec"
"path/filepath"
)
/*
execDocker is a pretty wrapper around exec.Command("docker",...)
*/
func execDocker(path string, command string, args ...string) (output string, err error) {
// Hold the output from our command
var outputBytes []byte
// First, we create an array with command and args to pass to exec
tmp := []string{command}
for _, arg := range args {
tmp = append(tmp, arg)
}
// Next, we build and execute the command
cmd := exec.Command("docker", tmp...)
cmd.Dir, err = filepath.Abs(path)
if err != nil {
return
}
outputBytes, err = cmd.CombinedOutput()
output = string(outputBytes)
return
}
type DockerOpts struct {
Cache bool
}
/*
buildImage will take a path to a docker image, and execute docker build as a
child process. It will tag the docker built image as name, this allows us to
later build other images using this one as a base. It captures stdout and
stderr returning them both in output.
*/
func buildImage(name string, path string, opts DockerOpts) (output string, err error) {
args := []string{"-t", name}
if !opts.Cache {
args = append(args, "--no-cache")
}
// local directory
args = append(args, ".")
return execDocker(path, "build", args...)
}
/*
pushImage will take a docker image and push it to a remote registry. It captures
stdout and stderr returning them both in output
*/
func pushImage(name string) (output string, err error) {
return execDocker("/", "push", name)
}
func dockerAlias(name string, alias string) (output string, err error) {
return execDocker("/", "tag", "-f", name, alias)
}