Skip to content

Commit 868e996

Browse files
milasdancysoft
andauthored
model: add remove() to Image (#3026)
Allow an Image to be deleted by calling the remove() method on it, just like a Volume. Signed-off-by: Ahmon Dancy <[email protected]> Signed-off-by: Milas Bowman <[email protected]> Co-authored-by: Ahmon Dancy <[email protected]>
1 parent 26753c8 commit 868e996

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docker/models/images.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ def history(self):
6161
"""
6262
return self.client.api.history(self.id)
6363

64+
def remove(self, force=False, noprune=False):
65+
"""
66+
Remove this image.
67+
68+
Args:
69+
force (bool): Force removal of the image
70+
noprune (bool): Do not delete untagged parents
71+
72+
Raises:
73+
:py:class:`docker.errors.APIError`
74+
If the server returns an error.
75+
"""
76+
return self.client.api.remove_image(
77+
self.id,
78+
force=force,
79+
noprune=noprune,
80+
)
81+
6482
def save(self, chunk_size=DEFAULT_DATA_CHUNK_SIZE, named=False):
6583
"""
6684
Get a tarball of an image. Similar to the ``docker save`` command.

tests/unit/models_images_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ def test_history(self):
150150
image.history()
151151
client.api.history.assert_called_with(FAKE_IMAGE_ID)
152152

153+
def test_remove(self):
154+
client = make_fake_client()
155+
image = client.images.get(FAKE_IMAGE_ID)
156+
image.remove()
157+
client.api.remove_image.assert_called_with(
158+
FAKE_IMAGE_ID,
159+
force=False,
160+
noprune=False,
161+
)
162+
153163
def test_save(self):
154164
client = make_fake_client()
155165
image = client.images.get(FAKE_IMAGE_ID)

0 commit comments

Comments
 (0)