Skip to content

Commit

Permalink
Improve the robustness of image cleanup (#313)
Browse files Browse the repository at this point in the history
* improve robustness of image cleanup

* Don't automatically clean up a named image
That makes the name useless

* Remove unused output_callback

As flagged in the review
  • Loading branch information
tfoote authored Feb 3, 2025
1 parent 66fe159 commit 149d4ef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/rocker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def main():
exit_code = dig.build(**vars(args))
if exit_code != 0:
print("Build failed exiting")
if not args_dict['persist_image']:
if not (args_dict['persist_image'] or args_dict.get('image_name')):
dig.clear_image()
return exit_code
# Convert command into string
args.command = ' '.join(args.command)
result = dig.run(**args_dict)
if not args_dict['persist_image']:
if not (args_dict['persist_image'] or args_dict.get('image_name')):
print(f'Clearing Image: {dig.image_id}s\nTo not clean up use --persist-images')
dig.clear_image()
return result
Expand Down
18 changes: 15 additions & 3 deletions src/rocker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,23 @@ def docker_build(docker_client = None, output_callback = None, **kwargs):
print("no more output and success not detected")
return None

def docker_remove_image(image_id, docker_client = None, output_callback = None, **kwargs):
def docker_remove_image(
image_id,
docker_client = None,
fail_on_error = False,
force = False,
**kwargs):

if not docker_client:
docker_client = get_docker_client()

docker_client.remove_image(image_id)
try:
docker_client.remove_image(image_id, force=force)
except docker.errors.APIError as ex:
## removing the image can fail if there's child images
if fail_on_error:
return False
return True

class SIGWINCHPassthrough(object):
def __init__ (self, process):
Expand Down Expand Up @@ -426,7 +437,8 @@ def run(self, command='', **kwargs):

def clear_image(self):
if self.image_id:
docker_remove_image(self.image_id)
if not docker_remove_image(self.image_id):
print(f'Failed to clear image {self.image_id} it likely has child images.')
self.image_id = None
self.built = False

Expand Down

0 comments on commit 149d4ef

Please sign in to comment.