From be3a47e2febabd97a1eda8c95f1c99114118ffb4 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sun, 17 Feb 2019 20:28:40 +0100 Subject: [PATCH] docker_common: improve image finding methods (#44692) * Work around problem with Docker daemon that sometimes won't find image if prefixed with docker.io repo name. * When tring library/xxx, docker-py also doesn't sometimes find the image. * Add changelog. --- .../fragments/44692-docker-find-image.yaml | 2 ++ lib/ansible/module_utils/docker/common.py | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/44692-docker-find-image.yaml diff --git a/changelogs/fragments/44692-docker-find-image.yaml b/changelogs/fragments/44692-docker-find-image.yaml new file mode 100644 index 00000000000..d41b711dfa1 --- /dev/null +++ b/changelogs/fragments/44692-docker-find-image.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container, docker_image, docker_image_facts - also find local image when image name is prefixed with ``docker.io/library/`` or ``docker.io/``." diff --git a/lib/ansible/module_utils/docker/common.py b/lib/ansible/module_utils/docker/common.py index 23ee070ac4c..4a9d1918681 100644 --- a/lib/ansible/module_utils/docker/common.py +++ b/lib/ansible/module_utils/docker/common.py @@ -570,10 +570,21 @@ class AnsibleDockerClient(Client): # In API <= 1.20 seeing 'docker.io/' as the name of images pulled from docker hub registry, repo_name = auth.resolve_repository_name(name) if registry == 'docker.io': - # the name does not contain a registry, so let's see if docker.io works - lookup = "docker.io/%s" % name - self.log("Check for docker.io image: %s" % lookup) - images = self._image_lookup(lookup, tag) + # If docker.io is explicitly there in name, the image + # isn't found in some cases (#41509) + self.log("Check for docker.io image: %s" % repo_name) + images = self._image_lookup(repo_name, tag) + if len(images) == 0 and repo_name.startswith('library/'): + # Sometimes library/xxx images are not found + lookup = repo_name[len('library/'):] + self.log("Check for docker.io image: %s" % lookup) + images = self._image_lookup(lookup, tag) + if len(images) == 0: + # Last case: if docker.io wasn't there, it can be that + # the image wasn't found either (#15586) + lookup = "%s/%s" % (registry, repo_name) + self.log("Check for docker.io image: %s" % lookup) + images = self._image_lookup(lookup, tag) if len(images) > 1: self.fail("Registry returned more than one result for %s:%s" % (name, tag))