diff --git a/changelogs/fragments/47712-docker_container-detach-auto-remove.yml b/changelogs/fragments/47712-docker_container-detach-auto-remove.yml new file mode 100644 index 00000000000..728218fb5cd --- /dev/null +++ b/changelogs/fragments/47712-docker_container-detach-auto-remove.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - fixing race condition when ``detach`` and ``auto_remove`` are both ``true``." diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py index aa6ceb13437..bac7630c614 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -32,7 +32,7 @@ HAS_DOCKER_ERROR = None try: from requests.exceptions import SSLError from docker import __version__ as docker_version - from docker.errors import APIError, TLSParameterError + from docker.errors import APIError, NotFound, TLSParameterError from docker.tls import TLSConfig from docker import auth @@ -112,6 +112,9 @@ if not HAS_DOCKER_PY: class APIError(Exception): # noqa: F811 pass + class NotFound(Exception): # noqa: F811 + pass + def is_image_name_id(name): """Checks whether the given image name is in fact an image ID (hash).""" @@ -442,6 +445,8 @@ class AnsibleDockerClient(Client): self.log("Inspecting container Id %s" % result['Id']) result = self.inspect_container(container=result['Id']) self.log("Completed container inspection") + except NotFound as exc: + return None except Exception as exc: self.fail("Error inspecting container: %s" % exc)