Skip to content

Commit

Permalink
remove stopped containers on --remove-orphans
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
collin5 committed Jan 25, 2019
1 parent 8edb0d8 commit c27132a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compose/project.py
Expand Up @@ -627,7 +627,7 @@ def matches_service_names(container):

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:
Expand All @@ -638,7 +638,10 @@ def _find():
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(
Expand Down
17 changes: 17 additions & 0 deletions tests/acceptance/cli_test.py
Expand Up @@ -12,6 +12,7 @@
import time
from collections import Counter
from collections import namedtuple
from functools import reduce
from operator import attrgetter

import pytest
Expand Down Expand Up @@ -1099,6 +1100,22 @@ def test_up_no_start(self):
]
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'
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/v2-simple/one-container.yml
@@ -0,0 +1,5 @@
version: "2"
services:
simple:
image: busybox:latest
command: top

0 comments on commit c27132a

Please sign in to comment.