haveImage reports whether we have the the given docker image. The name can either be of the , or , or form.
(name string)
| 133 | // haveImage reports whether we have the the given docker image. The name can |
| 134 | // either be of the <repository>, or <image id>, or <repository:tag> form. |
| 135 | func haveImage(name string) (ok bool, err error) { |
| 136 | out, err := exec.Command("docker", "images", "--no-trunc").Output() |
| 137 | if err != nil { |
| 138 | return |
| 139 | } |
| 140 | fields := strings.Split(name, ":") |
| 141 | if len(fields) < 2 { |
| 142 | return bytes.Contains(out, []byte(name)), nil |
| 143 | } |
| 144 | tag := fields[1] |
| 145 | image := fields[0] |
| 146 | sc := bufio.NewScanner(bytes.NewBuffer(out)) |
| 147 | for sc.Scan() { |
| 148 | l := sc.Text() |
| 149 | if !strings.HasPrefix(l, image) { |
| 150 | continue |
| 151 | } |
| 152 | if strings.HasPrefix(strings.TrimSpace(strings.TrimPrefix(l, image)), tag) { |
| 153 | return true, nil |
| 154 | } |
| 155 | } |
| 156 | return false, sc.Err() |
| 157 | } |
| 158 | |
| 159 | func run(args ...string) (containerID string, err error) { |
| 160 | cmd := exec.Command("docker", append([]string{"run"}, args...)...) |