Skip to content

Commit a154092

Browse files
committed
Add prune_containers method
Signed-off-by: Joffrey F <[email protected]>
1 parent 2d9f5bd commit a154092

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

docker/api/container.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -911,16 +911,35 @@ def put_archive(self, container, path, data):
911911
Raises:
912912
:py:class:`docker.errors.APIError`
913913
If the server returns an error.
914-
915-
Raises:
916-
:py:class:`~docker.errors.APIError` If an error occurs.
917914
"""
918915
params = {'path': path}
919916
url = self._url('/containers/{0}/archive', container)
920917
res = self._put(url, params=params, data=data)
921918
self._raise_for_status(res)
922919
return res.status_code == 200
923920

921+
@utils.minimum_version('1.25')
922+
def prune_containers(self, filters=None):
923+
"""
924+
Delete stopped containers
925+
926+
Args:
927+
filters (dict): Filters to process on the prune list.
928+
929+
Returns:
930+
(dict): A dict containing a list of deleted container IDs and
931+
the amount of disk space reclaimed in bytes.
932+
933+
Raises:
934+
:py:class:`docker.errors.APIError`
935+
If the server returns an error.
936+
"""
937+
params = {}
938+
if filters:
939+
params['filters'] = utils.convert_filters(filters)
940+
url = self._url('/containers/prune')
941+
return self._result(self._post(url, params=params), True)
942+
924943
@utils.check_resource
925944
def remove_container(self, container, v=False, link=False, force=False):
926945
"""

docker/models/containers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,23 @@ def list(self, all=False, before=None, filters=None, limit=-1, since=None):
763763
since=since)
764764
return [self.get(r['Id']) for r in resp]
765765

766+
def prune(self, filters=None):
767+
"""
768+
Delete stopped containers
769+
770+
Args:
771+
filters (dict): Filters to process on the prune list.
772+
773+
Returns:
774+
(dict): A dict containing a list of deleted container IDs and
775+
the amount of disk space reclaimed in bytes.
776+
777+
Raises:
778+
:py:class:`docker.errors.APIError`
779+
If the server returns an error.
780+
"""
781+
return self.client.api.prune_containers(filters=filters)
782+
766783

767784
# kwargs to copy straight from run to create
768785
RUN_CREATE_KWARGS = [

tests/integration/api_container_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,20 @@ def test_pause_unpause(self):
10941094
self.assertEqual(state['Paused'], False)
10951095

10961096

1097+
class PruneTest(BaseAPIIntegrationTest):
1098+
@requires_api_version('1.25')
1099+
def test_prune_containers(self):
1100+
container1 = self.client.create_container(BUSYBOX, ['echo', 'hello'])
1101+
container2 = self.client.create_container(BUSYBOX, ['sleep', '9999'])
1102+
self.client.start(container1)
1103+
self.client.start(container2)
1104+
self.client.wait(container1)
1105+
result = self.client.prune_containers()
1106+
assert container1['Id'] in result['ContainersDeleted']
1107+
assert result['SpaceReclaimed'] > 0
1108+
assert container2['Id'] not in result['ContainersDeleted']
1109+
1110+
10971111
class GetContainerStatsTest(BaseAPIIntegrationTest):
10981112
@requires_api_version('1.19')
10991113
def test_get_container_stats_no_stream(self):

0 commit comments

Comments
 (0)