Skip to content

Commit 6d8dff3

Browse files
committed
Add prune_images method
Signed-off-by: Joffrey F <[email protected]>
1 parent a154092 commit 6d8dff3

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

docker/api/image.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,31 @@ def load_image(self, data):
274274
res = self._post(self._url("/images/load"), data=data)
275275
self._raise_for_status(res)
276276

277+
@utils.minimum_version('1.25')
278+
def prune_images(self, filters=None):
279+
"""
280+
Delete unused images
281+
282+
Args:
283+
filters (dict): Filters to process on the prune list.
284+
Available filters:
285+
- dangling (bool): When set to true (or 1), prune only
286+
unused and untagged images.
287+
288+
Returns:
289+
(dict): A dict containing a list of deleted image IDs and
290+
the amount of disk space reclaimed in bytes.
291+
292+
Raises:
293+
:py:class:`docker.errors.APIError`
294+
If the server returns an error.
295+
"""
296+
url = self._url("/images/prune")
297+
params = {}
298+
if filters is not None:
299+
params['filters'] = utils.convert_filters(filters)
300+
return self._result(self._post(url, params=params), True)
301+
277302
def pull(self, repository, tag=None, stream=False,
278303
insecure_registry=False, auth_config=None, decode=False):
279304
"""

docker/models/containers.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22

3+
from ..api import APIClient
34
from ..errors import (ContainerError, ImageNotFound,
45
create_unexpected_kwargs_error)
56
from ..types import HostConfig
@@ -764,21 +765,8 @@ def list(self, all=False, before=None, filters=None, limit=-1, since=None):
764765
return [self.get(r['Id']) for r in resp]
765766

766767
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-
"""
781768
return self.client.api.prune_containers(filters=filters)
769+
prune.__doc__ = APIClient.prune_containers.__doc__
782770

783771

784772
# kwargs to copy straight from run to create

docker/models/images.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,7 @@ def remove(self, *args, **kwargs):
269269
def search(self, *args, **kwargs):
270270
return self.client.api.search(*args, **kwargs)
271271
search.__doc__ = APIClient.search.__doc__
272+
273+
def prune(self, filters=None):
274+
return self.client.api.prune_images(filters=filters)
275+
prune.__doc__ = APIClient.prune_images.__doc__

tests/integration/api_image_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import docker
1616

17+
from ..helpers import requires_api_version
1718
from .base import BaseAPIIntegrationTest, BUSYBOX
1819

1920

@@ -285,3 +286,18 @@ def test_import_from_url(self):
285286
self.assertIn('status', result)
286287
img_id = result['status']
287288
self.tmp_imgs.append(img_id)
289+
290+
291+
@requires_api_version('1.25')
292+
class PruneImagesTest(BaseAPIIntegrationTest):
293+
def test_prune_images(self):
294+
try:
295+
self.client.remove_image('hello-world')
296+
except docker.errors.APIError:
297+
pass
298+
self.client.pull('hello-world')
299+
self.tmp_imgs.append('hello-world')
300+
img_id = self.client.inspect_image('hello-world')['Id']
301+
result = self.client.prune_images()
302+
assert img_id in result['ImagesDeleted']
303+
assert result['SpaceReclaimed'] > 0

0 commit comments

Comments
 (0)