docker-image-cleanup 0.1.3__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {docker_image_cleanup-0.1.3 → docker_image_cleanup-0.1.4}/PKG-INFO +1 -2
- {docker_image_cleanup-0.1.3 → docker_image_cleanup-0.1.4}/README.md +0 -1
- {docker_image_cleanup-0.1.3 → docker_image_cleanup-0.1.4}/docker_image_cleanup/__init__.py +17 -11
- {docker_image_cleanup-0.1.3 → docker_image_cleanup-0.1.4}/pyproject.toml +1 -1
- {docker_image_cleanup-0.1.3 → docker_image_cleanup-0.1.4}/docker_image_cleanup/version.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: docker-image-cleanup
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Clean up old Docker images from local storage with retention controls
|
|
5
5
|
Keywords: docker,cleanup,images,disk-space
|
|
6
6
|
Author: Michael Bianco
|
|
@@ -132,4 +132,3 @@ Images with multiple tags are handled intelligently. If all tags on an image wou
|
|
|
132
132
|
---
|
|
133
133
|
|
|
134
134
|
*This project was created from [iloveitaly/python-package-template](https://github.com/iloveitaly/python-package-template)*
|
|
135
|
-
> theirs
|
|
@@ -113,7 +113,7 @@ def determine_cleanup_actions(
|
|
|
113
113
|
if img.created >= min_age_threshold:
|
|
114
114
|
keep_tags.update(img.tags)
|
|
115
115
|
|
|
116
|
-
|
|
116
|
+
images_to_delete: dict[str, int] = {}
|
|
117
117
|
tags_to_remove: list[str] = []
|
|
118
118
|
|
|
119
119
|
images_by_id: dict[str, list[ImageInfo]] = defaultdict(list)
|
|
@@ -142,7 +142,7 @@ def determine_cleanup_actions(
|
|
|
142
142
|
if set(tags_for_this_image_to_remove) == current_image_repo_tags:
|
|
143
143
|
img_info = img_list[0]
|
|
144
144
|
if not img_info.is_in_use:
|
|
145
|
-
|
|
145
|
+
images_to_delete[img_id] = img_info.size
|
|
146
146
|
total_size_saved += img_info.size
|
|
147
147
|
else:
|
|
148
148
|
log.warning(
|
|
@@ -152,7 +152,7 @@ def determine_cleanup_actions(
|
|
|
152
152
|
)
|
|
153
153
|
|
|
154
154
|
return {
|
|
155
|
-
"images_to_delete":
|
|
155
|
+
"images_to_delete": images_to_delete,
|
|
156
156
|
"tags_to_remove": list(set(tags_to_remove)),
|
|
157
157
|
"total_size_saved": total_size_saved,
|
|
158
158
|
}
|
|
@@ -160,7 +160,7 @@ def determine_cleanup_actions(
|
|
|
160
160
|
|
|
161
161
|
def execute_cleanup(
|
|
162
162
|
client: docker.DockerClient,
|
|
163
|
-
images_to_delete:
|
|
163
|
+
images_to_delete: dict[str, int],
|
|
164
164
|
tags_to_remove: list[str],
|
|
165
165
|
total_size_saved: int,
|
|
166
166
|
dry_run: bool,
|
|
@@ -172,7 +172,7 @@ def execute_cleanup(
|
|
|
172
172
|
if tags_to_remove:
|
|
173
173
|
log.info("would remove tags", tags=tags_to_remove)
|
|
174
174
|
if images_to_delete:
|
|
175
|
-
log.info("would remove images", image_ids=images_to_delete)
|
|
175
|
+
log.info("would remove images", image_ids=list(images_to_delete.keys()))
|
|
176
176
|
|
|
177
177
|
log.info(
|
|
178
178
|
"total space that would be saved",
|
|
@@ -184,19 +184,25 @@ def execute_cleanup(
|
|
|
184
184
|
for tag in tags_to_remove:
|
|
185
185
|
try:
|
|
186
186
|
client.images.remove(tag, force=False)
|
|
187
|
-
log.info("
|
|
187
|
+
log.info("removed tag", tag=tag)
|
|
188
188
|
except docker.errors.APIError as e:
|
|
189
|
-
log.warning("skipped
|
|
189
|
+
log.warning("skipped tag removal", tag=tag, reason=str(e))
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
# We explicitly remove the Image ID here even after untagging.
|
|
192
|
+
# This is primarily to catch and clean up "dangling" images (images with no tags)
|
|
193
|
+
# that might have been orphaned but wouldn't be caught by the tag loop.
|
|
194
|
+
for img_id, size in images_to_delete.items():
|
|
192
195
|
try:
|
|
193
|
-
img = client.images.get(img_id)
|
|
194
|
-
size = img.attrs.get("Size", 0)
|
|
195
196
|
client.images.remove(img_id, force=False)
|
|
196
197
|
log.info("removed image", image_id=img_id, size=format_size(size))
|
|
197
198
|
executed_size_saved += size
|
|
198
199
|
except docker.errors.ImageNotFound:
|
|
199
|
-
|
|
200
|
+
# If the image is not found here, it means Docker automatically deleted
|
|
201
|
+
# the underlying image data when we removed its last tag in the loop above.
|
|
202
|
+
log.info(
|
|
203
|
+
"image removed via untagging", image_id=img_id, size=format_size(size)
|
|
204
|
+
)
|
|
205
|
+
executed_size_saved += size
|
|
200
206
|
except docker.errors.APIError as e:
|
|
201
207
|
log.warning("skipped image removal", image_id=img_id, reason=str(e))
|
|
202
208
|
|
|
File without changes
|