From 08324198da0c9e6d998ef8472b99c33f73330ee4 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 29 Oct 2018 09:22:52 +0100 Subject: [PATCH] docker_container: improve race condition behavior for detach:no, auto_remove:yes behavior (#47712) * Don't die when get_container is called for container which is terminating during get_container call. If it terminates between client.containers() and client.inspect_container(), the module will fail with an error such as Error inspecting container: 404 Client Error: Not Found ("No such container: xxx") * Add changelog. (cherry picked from commit b9706e2ff575509308ef1cd90d83e58e613cf838) --- .../47712-docker_container-detach-auto-remove.yml | 2 ++ lib/ansible/module_utils/docker_common.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/47712-docker_container-detach-auto-remove.yml 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 aad9c69cccf..71819d9800a 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -35,7 +35,7 @@ HAS_DOCKER_ERROR = None try: from requests.exceptions import SSLError from docker import __version__ as docker_version - from docker.errors import APIError, TLSParameterError, NotFound + from docker.errors import APIError, NotFound, TLSParameterError from docker.tls import TLSConfig from docker.constants import DEFAULT_DOCKER_API_VERSION from docker import auth @@ -119,6 +119,9 @@ if not HAS_DOCKER_PY: class APIError(Exception): pass + class NotFound(Exception): # noqa: F811 + pass + def sanitize_result(data): """Sanitize data object for return to Ansible. @@ -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)