This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
docker.go
85 lines (68 loc) · 2.35 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
73
74
75
76
77
78
79
80
81
82
83
84
85
package sti
import (
"log"
"github.com/fsouza/go-dockerclient"
)
// Determines whether the supplied image is in the local registry.
func (h requestHandler) isImageInLocalRegistry(imageName string) (bool, error) {
image, err := h.dockerClient.InspectImage(imageName)
if image != nil {
return true, nil
} else if err == docker.ErrNoSuchImage {
return false, nil
}
return false, err
}
// Pull an image into the local registry
func (h requestHandler) checkAndPull(imageName string) (*docker.Image, error) {
image, err := h.dockerClient.InspectImage(imageName)
if err != nil && err != docker.ErrNoSuchImage {
//TODO should this be a different error?
return nil, ErrPullImageFailed
}
if image == nil {
log.Printf("Pulling image %s\n", imageName)
err = h.dockerClient.PullImage(docker.PullImageOptions{Repository: imageName}, docker.AuthConfiguration{})
if err != nil {
return nil, ErrPullImageFailed
}
image, err = h.dockerClient.InspectImage(imageName)
if err != nil {
return nil, err
}
} else if h.request.Verbose {
log.Printf("Image %s available locally\n", imageName)
}
return image, nil
}
// Creates a container from a given image name and returns the ID of the created container.
func (h requestHandler) containerFromImage(imageName string) (*docker.Container, error) {
config := docker.Config{Image: imageName, AttachStdout: false, AttachStderr: false, Cmd: []string{"/bin/true"}}
container, err := h.dockerClient.CreateContainer(docker.CreateContainerOptions{Name: "", Config: &config})
if err != nil {
return nil, err
}
err = h.dockerClient.StartContainer(container.ID, &docker.HostConfig{})
if err != nil {
return nil, err
}
exitCode, err := h.dockerClient.WaitContainer(container.ID)
if err != nil {
return nil, err
}
if exitCode != 0 {
log.Printf("Container exit code: %d\n", exitCode)
return nil, ErrCreateContainerFailed
}
return container, nil
}
// Remove a container and its associated volumes.
func (h requestHandler) removeContainer(id string) {
h.dockerClient.RemoveContainer(docker.RemoveContainerOptions{id, true, true})
}
// Commit the container with the given ID with the given tag.
func (h requestHandler) commitContainer(id, tag string) error {
// TODO: commit message / author?
_, err := h.dockerClient.CommitContainer(docker.CommitContainerOptions{Container: id, Repository: tag})
return err
}