From ae16ff6083b8f063ac6fdec8581fd3363264a7c7 Mon Sep 17 00:00:00 2001 From: Tully Foote Date: Fri, 11 Nov 2022 12:55:29 -0800 Subject: [PATCH] Name the cached images to make it easier to clean up the cached images Signed-off-by: Tully Foote --- README.md | 7 +++++++ src/rocker/core.py | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d1cd5fa..673563b8 100644 --- a/README.md +++ b/README.md @@ -212,3 +212,10 @@ It will process the same as `docker`'s `--volume` option, `rocker --volume` take - 2nd field: (optional) the path where the file or directory is mounted in the container. - If only the 1st field is supplied, same value as the 1st field will be populated as the 2nd field. - 3rd field: (optional) bind propagation as `ro`, `z`, and `Z`. See [docs.docker.com](https://docs.docker.com/storage/bind-mounts/) for further detail. + + +## Helpful tips + +Clean up the rocker cache + + `docker images | grep rocker_cache | awk '{ printf $1":"$2; print"" }' | xargs docker rmi` \ No newline at end of file diff --git a/src/rocker/core.py b/src/rocker/core.py index dad8a11a..e44a967c 100644 --- a/src/rocker/core.py +++ b/src/rocker/core.py @@ -14,6 +14,7 @@ from collections import OrderedDict import io +from hashlib import md5 import os import re import sys @@ -225,8 +226,15 @@ def build(self, **kwargs): arguments['pull'] = kwargs.get('pull', False) image_name = kwargs.get('image_name', None) if image_name: - print(f"Running docker tag {self.image_id} {image_name}") arguments['tag'] = image_name + else: + kwhash = self.get_argument_hash(kwargs) + #TODO(tfoote) Only flag extensions in the image (aka skip runtime only) + tag = 'rocker_cache:' + kwargs.get('image', '').replace(':', '_') + '_extensions__' + '_'.join([x.name for x in self.active_extensions]) + '__arghash' + str(kwhash) + arguments['tag'] = tag + + print(f"{kwargs} Generated Image Tagged: {tag}") + print("Building docker file with arguments: ", arguments) try: self.image_id = docker_build( @@ -243,6 +251,15 @@ def build(self, **kwargs): print("Docker build failed\n", ex) return 1 + def get_argument_hash(self, kwargs): + """ Compute the has of the arguments excluding elements not included + in the image such as the command + """ + # TODO(tfoote) flag specific keywords as being in the image vs only runtime + invariant = kwargs + invariant.pop('command') + return md5(str(invariant).encode()).hexdigest() + def get_operating_mode(self, args): operating_mode = args.get('mode') # Default to non-interactive if unset