From bc4e49734be5d73f485b30f49f97c00d26a6d156 Mon Sep 17 00:00:00 2001 From: aviau Date: Fri, 22 Jan 2021 09:19:29 -0500 Subject: [PATCH] images/haveImageID: use docker images, not inspect Docker inspect will not exit 0 if no version of the image is available on the machine, leading to the method failing. There is no flag to adjust this in docker inspect, the only solution would be to parse the error message but that would be fragile. Instead, we opt to use `docker images` which has a very similar output with the exception that it will list all tags available for that image and that it will not fail on absent images. --- internal/images/docker.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/images/docker.go b/internal/images/docker.go index b150f3b2b..667363b1d 100644 --- a/internal/images/docker.go +++ b/internal/images/docker.go @@ -17,6 +17,7 @@ limitations under the License. package images // import "perkeep.org/internal/images" import ( + "bufio" "bytes" "errors" "fmt" @@ -66,12 +67,26 @@ func haveDocker() bool { } func haveImageID(name, id string) (ok bool, err error) { - out, err := exec.Command("docker", "inspect", "-f", "{{.Id}}", name).Output() + out, err := exec.Command( + "docker", + "images", + "--quiet", + "--no-trunc", + "--format", + "{{.ID}}", + name, + ).Output() if err != nil { return false, err } - have := strings.TrimSpace(string(out)) - return have == id, nil + scanner := bufio.NewScanner(bytes.NewReader(out)) + for scanner.Scan() { + line := strings.TrimSpace((scanner.Text())) + if line == id { + return true, nil + } + } + return false, nil } // Pull retrieves the docker image with 'docker pull'.