Skip to content

Commit d51a2f3

Browse files
Untag instead of force remove image for podman (#1342)
now cleanup_images will behave the same for podman and docker
1 parent aa9bbe0 commit d51a2f3

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/ansible_runner/cleanup.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,29 @@ def cleanup_dirs(pattern: str, exclude_strings: list | None = None, grace_period
152152

153153

154154
def cleanup_images(images: list, runtime: str) -> int:
155-
"""Note: docker will just untag while podman will remove layers with same command"""
155+
"""
156+
`docker rmi` will just untag while
157+
`podman rmi` will untag and remove layers and cause runing container to be killed
158+
for podman we use `untag` to achieve the same behavior
159+
160+
NOTE: this only untag the image and does not delete the image prune_images need to be call to delete
161+
"""
156162
rm_ct = 0
157163
for image_tag in images:
158164
stdout = run_command([runtime, 'images', '--format="{{.Repository}}:{{.Tag}}"', image_tag])
159165
if not stdout:
160166
continue
161167
for discovered_tag in stdout.split('\n'):
162-
stdout = run_command([runtime, 'rmi', discovered_tag.strip().strip('"'), '-f'])
163-
rm_ct += stdout.count('Untagged:')
168+
if runtime == 'podman':
169+
try:
170+
stdout = run_command([runtime, 'untag', image_tag])
171+
if not stdout:
172+
rm_ct += 1
173+
except Exception:
174+
pass # best effort untag
175+
else:
176+
stdout = run_command([runtime, 'rmi', discovered_tag.strip().strip('"'), '-f'])
177+
rm_ct += stdout.count('Untagged:')
164178
return rm_ct
165179

166180

0 commit comments

Comments
 (0)