Remove dangling images and leftover containers from the local Docker engine.
Examples:
>>> from ures.docker import DockerCleanup
>>> cleanup = DockerCleanup()
>>> cleanup.dangling_images()
>>> cleanup.stopped_containers()
Attach to a Docker client.
Parameters:
-
client
(DockerClient | None, default:
None
)
–
A Docker SDK client. Defaults to
docker.from_env().
Source code in ures/docker/cleanup.py
| def __init__(self, client: Optional[docker.DockerClient] = None):
"""Attach to a Docker client.
Args:
client (docker.DockerClient | None): A Docker SDK client. Defaults to
``docker.from_env()``.
"""
self.client = client or docker.from_env()
|
dangling_images
Delete dangling (untagged) local images.
Source code in ures/docker/cleanup.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | def dangling_images(self):
"""Delete dangling (untagged) local images."""
client = self.client
# List dangling images (dangling=true filter)
dangling_images = client.images.list(filters={"dangling": True})
if not dangling_images:
print("No dangling images to remove.")
return
for image in dangling_images:
try:
print(f"Removing image: {image.id}")
client.images.remove(image.id)
except Exception as e:
print(f"Error removing image {image.id}: {e}")
print("Dangling images prune complete!")
|
stopped_containers
Delete containers in exited or created state.
Source code in ures/docker/cleanup.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 | def stopped_containers(self):
"""Delete containers in ``exited`` or ``created`` state."""
client = self.client
stopped_containers = client.containers.list(
all=True, filters={"status": "exited"}
)
just_created_containers = client.containers.list(
all=True, filters={"status": "created"}
)
plan2remove_containers = stopped_containers + just_created_containers
if not plan2remove_containers:
print("No stopped xmem_container to prune.")
return
for container in plan2remove_containers:
try:
print(f"Removing container: {container.name} ({container.short_id})")
container.remove()
except Exception as e:
print(f"Error removing container {container.name}: {e}")
print("Pruning complete!")
|