1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Examples demonstrating system cleanup and maintenance operations.
//!
//! This example shows how to use Docker system prune, df, and other
//! cleanup commands to manage Docker resources.
use docker_wrapper::{ContainerPruneCommand, ImagePruneCommand, SystemDfCommand};
#[tokio::main]
async fn main() {
println!("Docker System Cleanup Examples");
println!("==============================\n");
// Example 1: Check disk usage
println!("1. Checking Docker disk usage:");
match SystemDfCommand::new().verbose().run().await {
Ok(usage) => {
println!("Docker Disk Usage:");
println!(" Images: {} items", usage.images.len());
println!(" Containers: {} items", usage.containers.len());
println!(" Volumes: {} items", usage.volumes.len());
println!(" Build Cache: {} items", usage.build_cache.len());
}
Err(e) => {
println!("Failed to get disk usage: {e}");
}
}
println!();
// Example 2: Prune stopped containers
println!("2. Pruning stopped containers:");
match ContainerPruneCommand::new().force().run().await {
Ok(result) => {
println!(
"Removed {} containers, reclaimed {} bytes",
result.containers_deleted.len(),
result.space_reclaimed
);
}
Err(e) => {
println!("Failed to prune containers: {e}");
}
}
println!();
// Example 3: Prune dangling images
println!("3. Pruning dangling images:");
match ImagePruneCommand::new().dangling_only().force().run().await {
Ok(result) => {
println!(
"Removed {} images, reclaimed {} bytes",
result.images_deleted.len(),
result.space_reclaimed
);
}
Err(e) => {
println!("Failed to prune images: {e}");
}
}
println!();
// Example 4: System-wide prune (be careful!)
println!("4. System-wide prune (dry run - not executing):");
println!(" Would run: docker system prune --all --volumes --force");
println!(" This would remove:");
println!(" - All stopped containers");
println!(" - All networks not used by at least one container");
println!(" - All images without at least one container");
println!(" - All build cache");
println!(" - All volumes not used by at least one container");
// Uncomment to actually run (WARNING: destructive!)
// match SystemPruneCommand::new()
// .all()
// .volumes()
// .force()
// .run()
// .await
// {
// Ok(result) => {
// println!("System prune complete:");
// println!(" Containers removed: {}", result.containers_deleted.len());
// println!(" Images removed: {}", result.images_deleted.len());
// println!(" Networks removed: {}", result.networks_deleted.len());
// println!(" Volumes removed: {}", result.volumes_deleted.len());
// println!(" Total space reclaimed: {} bytes", result.space_reclaimed);
// }
// Err(e) => {
// println!("Failed to prune system: {e}");
// }
// }
println!();
// Example 5: Prune with filters
println!("5. Prune containers older than 24 hours:");
match ContainerPruneCommand::new()
.until("24h")
.force()
.run()
.await
{
Ok(result) => {
println!(
"Removed {} old containers, reclaimed {} bytes",
result.containers_deleted.len(),
result.space_reclaimed
);
}
Err(e) => {
println!("Failed to prune old containers: {e}");
}
}
println!();
// Example 6: Label-based cleanup
println!("6. Prune containers with specific labels:");
match ContainerPruneCommand::new()
.with_label("cleanup", Some("true"))
.force()
.run()
.await
{
Ok(result) => {
println!(
"Removed {} labeled containers, reclaimed {} bytes",
result.containers_deleted.len(),
result.space_reclaimed
);
}
Err(e) => {
println!("Failed to prune labeled containers: {e}");
}
}
}