remove stopped containers on --remove-orphans

Signed-off-by: Collins Abitekaniza <abtcolns@gmail.com>

kill orphan containers, catch APIError Exception

Signed-off-by: Collins Abitekaniza <abtcolns@gmail.com>

test remove orphans with --no-start

Signed-off-by: Collins Abitekaniza <abtcolns@gmail.com>
This commit is contained in:
Collins Abitekaniza 2018-11-15 15:19:08 +03:00
parent 8edb0d872d
commit c27132afad
3 changed files with 27 additions and 2 deletions

View File

@ -627,7 +627,7 @@ class Project(object):
def find_orphan_containers(self, remove_orphans):
def _find():
containers = self._labeled_containers()
containers = set(self._labeled_containers() + self._labeled_containers(stopped=True))
for ctnr in containers:
service_name = ctnr.labels.get(LABEL_SERVICE)
if service_name not in self.service_names:
@ -638,7 +638,10 @@ class Project(object):
if remove_orphans:
for ctnr in orphans:
log.info('Removing orphan container "{0}"'.format(ctnr.name))
ctnr.kill()
try:
ctnr.kill()
except APIError:
pass
ctnr.remove(force=True)
else:
log.warning(

View File

@ -12,6 +12,7 @@ import subprocess
import time
from collections import Counter
from collections import namedtuple
from functools import reduce
from operator import attrgetter
import pytest
@ -1099,6 +1100,22 @@ class CLITestCase(DockerClientTestCase):
]
assert len(remote_volumes) > 0
@v2_only()
def test_up_no_start_remove_orphans(self):
self.base_dir = 'tests/fixtures/v2-simple'
self.dispatch(['up', '--no-start'], None)
services = self.project.get_services()
stopped = reduce((lambda prev, next: prev.containers(
stopped=True) + next.containers(stopped=True)), services)
assert len(stopped) == 2
self.dispatch(['-f', 'one-container.yml', 'up', '--no-start', '--remove-orphans'], None)
stopped2 = reduce((lambda prev, next: prev.containers(
stopped=True) + next.containers(stopped=True)), services)
assert len(stopped2) == 1
@v2_only()
def test_up_no_ansi(self):
self.base_dir = 'tests/fixtures/v2-simple'

View File

@ -0,0 +1,5 @@
version: "2"
services:
simple:
image: busybox:latest
command: top