docker_container: fix container removal when auto_remove is used (#48061)

* Fix container removal when auto_remove is used.

(cherry picked from commit af2b477950)
This commit is contained in:
Felix Fontein 2018-11-05 12:33:56 +01:00 committed by Toshio Kuratomi
parent 9ac8763df2
commit b0b9b4f7dd
2 changed files with 14 additions and 4 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "docker_container - do not fail when removing a container which has ``auto_remove: yes``."

View file

@ -646,6 +646,7 @@ try:
from docker.types import Ulimit, LogConfig from docker.types import Ulimit, LogConfig
else: else:
from docker.utils.types import Ulimit, LogConfig from docker.utils.types import Ulimit, LogConfig
from docker.errors import APIError, NotFound
except Exception as dummy: except Exception as dummy:
# missing docker-py handled in ansible.module_utils.docker # missing docker-py handled in ansible.module_utils.docker
pass pass
@ -2040,7 +2041,7 @@ class ContainerManager(DockerBaseClass):
if status != 0: if status != 0:
self.fail(output, status=status) self.fail(output, status=status)
if self.parameters.cleanup: if self.parameters.cleanup:
self.container_remove(container_id, force=True, ignore_failure=self.parameters.auto_remove) self.container_remove(container_id, force=True)
insp = self._get_container(container_id) insp = self._get_container(container_id)
if insp.raw: if insp.raw:
insp.raw['Output'] = output insp.raw['Output'] = output
@ -2049,7 +2050,7 @@ class ContainerManager(DockerBaseClass):
return insp return insp
return self._get_container(container_id) return self._get_container(container_id)
def container_remove(self, container_id, link=False, force=False, ignore_failure=False): def container_remove(self, container_id, link=False, force=False):
volume_state = (not self.parameters.keep_volumes) volume_state = (not self.parameters.keep_volumes)
self.log("remove container container:%s v:%s link:%s force%s" % (container_id, volume_state, link, force)) self.log("remove container container:%s v:%s link:%s force%s" % (container_id, volume_state, link, force))
self.results['actions'].append(dict(removed=container_id, volume_state=volume_state, link=link, force=force)) self.results['actions'].append(dict(removed=container_id, volume_state=volume_state, link=link, force=force))
@ -2058,9 +2059,16 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode: if not self.check_mode:
try: try:
response = self.client.remove_container(container_id, v=volume_state, link=link, force=force) response = self.client.remove_container(container_id, v=volume_state, link=link, force=force)
except Exception as exc: except NotFound as exc:
if not ignore_failure: pass
except APIError as exc:
if exc.response.status_code == 409 and ('removal of container ' in exc.explanation and
' is already in progress' in exc.explanation):
pass
else:
self.fail("Error removing container %s: %s" % (container_id, str(exc))) self.fail("Error removing container %s: %s" % (container_id, str(exc)))
except Exception as exc:
self.fail("Error removing container %s: %s" % (container_id, str(exc)))
return response return response
def container_update(self, container_id, update_parameters): def container_update(self, container_id, update_parameters):