Add --all option to rm command - remove one-off containers

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-03-15 17:00:24 -07:00 committed by Aanand Prasad
parent d5ef0dfa92
commit 187ea4cd81
4 changed files with 40 additions and 8 deletions

View File

@ -491,8 +491,12 @@ class TopLevelCommand(object):
Options:
-f, --force Don't ask to confirm removal
-v Remove volumes associated with containers
-a, --all Also remove one-off containers
"""
all_containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
all_containers = self.project.containers(
service_names=options['SERVICE'], stopped=True,
one_off=(None if options.get('--all') else False)
)
stopped_containers = [c for c in all_containers if not c.is_running]
if len(stopped_containers) > 0:
@ -501,7 +505,8 @@ class TopLevelCommand(object):
or yesno("Are you sure? [yN] ", default=False):
self.project.remove_stopped(
service_names=options['SERVICE'],
v=options.get('-v', False)
v=options.get('-v', False),
one_off=options.get('--all')
)
else:
print("No stopped containers")

View File

@ -47,10 +47,12 @@ class Project(object):
self.networks = networks or ProjectNetworks({}, False)
def labels(self, one_off=False):
return [
'{0}={1}'.format(LABEL_PROJECT, self.name),
'{0}={1}'.format(LABEL_ONE_OFF, "True" if one_off else "False"),
]
labels = ['{0}={1}'.format(LABEL_PROJECT, self.name)]
if one_off is not None:
labels.append(
'{0}={1}'.format(LABEL_ONE_OFF, "True" if one_off else "False")
)
return labels
@classmethod
def from_config(cls, name, config_data, client):
@ -249,8 +251,10 @@ class Project(object):
def kill(self, service_names=None, **options):
parallel.parallel_kill(self.containers(service_names), options)
def remove_stopped(self, service_names=None, **options):
parallel.parallel_remove(self.containers(service_names, stopped=True), options)
def remove_stopped(self, service_names=None, one_off=False, **options):
parallel.parallel_remove(self.containers(
service_names, stopped=True, one_off=(None if one_off else False)
), options)
def down(self, remove_image_type, include_volumes, remove_orphans=False):
self.stop()

View File

@ -17,6 +17,7 @@ Usage: rm [options] [SERVICE...]
Options:
-f, --force Don't ask to confirm removal
-v Remove volumes associated with containers
-a, --all Also remove one-off containers
```
Removes stopped service containers.

View File

@ -1125,6 +1125,28 @@ class CLITestCase(DockerClientTestCase):
self.dispatch(['rm', '-f'], None)
self.assertEqual(len(service.containers(stopped=True)), 0)
def test_rm_all(self):
service = self.project.get_service('simple')
service.create_container(one_off=False)
service.create_container(one_off=True)
kill_service(service)
self.assertEqual(len(service.containers(stopped=True)), 1)
self.assertEqual(len(service.containers(stopped=True, one_off=True)), 1)
self.dispatch(['rm', '-f'], None)
self.assertEqual(len(service.containers(stopped=True)), 0)
self.assertEqual(len(service.containers(stopped=True, one_off=True)), 1)
self.dispatch(['rm', '-f', '-a'], None)
self.assertEqual(len(service.containers(stopped=True, one_off=True)), 0)
service.create_container(one_off=False)
service.create_container(one_off=True)
kill_service(service)
self.assertEqual(len(service.containers(stopped=True)), 1)
self.assertEqual(len(service.containers(stopped=True, one_off=True)), 1)
self.dispatch(['rm', '-f', '--all'], None)
self.assertEqual(len(service.containers(stopped=True)), 0)
self.assertEqual(len(service.containers(stopped=True, one_off=True)), 0)
def test_stop(self):
self.dispatch(['up', '-d'], None)
service = self.project.get_service('simple')