Skip to content

Commit 709cdfc

Browse files
Untag instead of force remove image for podman (#1342) (#1344)
now cleanup_images will behave the same for podman and docker (cherry picked from commit d51a2f3) Co-authored-by: Hao Liu <44379968+TheRealHaoLiu@users.noreply.github.com>
1 parent c129775 commit 709cdfc

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

ansible_runner/cleanup.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,29 @@ def cleanup_dirs(pattern, exclude_strings=(), grace_period=GRACE_PERIOD_DEFAULT)
144144

145145

146146
def cleanup_images(images, runtime='podman'):
147-
"""Note: docker will just untag while podman will remove layers with same command"""
147+
"""
148+
`docker rmi` will just untag while
149+
`podman rmi` will untag and remove layers and cause runing container to be killed
150+
for podman we use `untag` to achieve the same behavior
151+
152+
NOTE: this only untag the image and does not delete the image prune_images need to be call to delete
153+
"""
148154
rm_ct = 0
149155
for image_tag in images:
150156
stdout = run_command([runtime, 'images', '--format="{{.Repository}}:{{.Tag}}"', image_tag])
151157
if not stdout:
152158
continue
153159
for discovered_tag in stdout.split('\n'):
154-
stdout = run_command([runtime, 'rmi', discovered_tag.strip().strip('"'), '-f'])
155-
rm_ct += stdout.count('Untagged:')
160+
if runtime == 'podman':
161+
try:
162+
stdout = run_command([runtime, 'untag', image_tag])
163+
if not stdout:
164+
rm_ct += 1
165+
except Exception:
166+
pass # best effort untag
167+
else:
168+
stdout = run_command([runtime, 'rmi', discovered_tag.strip().strip('"'), '-f'])
169+
rm_ct += stdout.count('Untagged:')
156170
return rm_ct
157171

158172

0 commit comments

Comments
 (0)