-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdocker_compose.rs
More file actions
234 lines (199 loc) · 7.61 KB
/
docker_compose.rs
File metadata and controls
234 lines (199 loc) · 7.61 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Docker Compose example demonstrating multi-container application management
//!
//! This example shows how to use the compose feature to manage
//! multi-container applications with Docker Compose.
//!
//! Run with: cargo run --example docker_compose --features compose
#[cfg(feature = "compose")]
use docker_wrapper::command::ComposeCommand;
#[cfg(feature = "compose")]
use docker_wrapper::compose::{
ComposeDownCommand, ComposeLogsCommand, ComposePsCommand, ComposeUpCommand, RemoveImages,
};
#[cfg(feature = "compose")]
use docker_wrapper::DockerCommand;
#[cfg(feature = "compose")]
use std::time::Duration;
#[cfg(not(feature = "compose"))]
fn main() {
println!("This example requires the 'compose' feature.");
println!("Run with: cargo run --example docker_compose --features compose");
}
#[cfg(feature = "compose")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Docker Compose Management Example");
println!("=================================\n");
// Example 1: Basic compose up/down workflow
println!("Example 1: Basic Compose Workflow");
println!("------------------------------------");
// Start services with compose up
println!("Starting services with compose up...");
let up_result = ComposeUpCommand::new()
.file("docker-compose.yml")
.project_name("my-app")
.detach() // Run in background
.build() // Build images if needed
.remove_orphans() // Clean up orphan containers
.wait() // Wait for services to be healthy
.wait_timeout(Duration::from_secs(60))
.execute()
.await;
match up_result {
Ok(result) => {
println!("Services started successfully");
println!("Output: {}", result.stdout.trim());
}
Err(e) => {
println!("Failed to start services: {}", e);
// Continue with example using a simulated environment
}
}
println!();
// Example 2: Check service status
println!("Example 2: Checking Service Status");
println!("-------------------------------------");
let ps_result = ComposePsCommand::new()
.file("docker-compose.yml")
.project_name("my-app")
.all() // Show all containers
.execute()
.await;
match ps_result {
Ok(result) => {
println!("Service status retrieved");
println!("Output: {}", result.stdout.trim());
}
Err(e) => println!("Could not retrieve service status: {}", e),
}
println!();
// Example 3: View logs
println!("Example 3: Viewing Service Logs");
println!("----------------------------------");
let logs_result = ComposeLogsCommand::new()
.file("docker-compose.yml")
.project_name("my-app")
.tail("20") // Last 20 lines
.timestamps() // Include timestamps
.no_color() // Disable colors for cleaner output
.service("web") // Specific service
.execute()
.await;
match logs_result {
Ok(result) => {
println!("Logs retrieved");
println!("Output: {}", result.stdout.trim());
}
Err(e) => println!("Could not retrieve logs: {}", e),
}
println!();
// Example 4: Advanced compose up with scaling
println!("Example 4: Advanced Compose Configuration");
println!("---------------------------------------------");
println!("Starting services with scaling...");
let scaled_up = ComposeUpCommand::new()
.file("docker-compose.yml")
.project_name("scaled-app")
.scale("web", 3) // Scale web service to 3 instances
.scale("worker", 2) // Scale worker to 2 instances
.detach()
.no_recreate() // Don't recreate existing containers
.execute()
.await;
match scaled_up {
Ok(_) => {
println!("Services scaled successfully");
println!(" web: 3 instances");
println!(" worker: 2 instances");
}
Err(e) => println!(
"Scaling failed (expected without valid compose file): {}",
e
),
}
println!();
// Example 5: Selective service management
println!("Example 5: Selective Service Management");
println!("------------------------------------------");
// Start only specific services
let selective_up = ComposeUpCommand::new()
.file("docker-compose.yml")
.service("database") // Only start database
.service("cache") // And cache
.no_deps() // Don't start dependent services
.detach()
.execute()
.await;
match selective_up {
Ok(_) => println!("Selected services started"),
Err(e) => println!(
"Selective start failed (expected without valid compose file): {}",
e
),
}
println!();
// Example 6: Clean shutdown
println!("Example 6: Clean Shutdown");
println!("----------------------------");
let down_result = ComposeDownCommand::new()
.file("docker-compose.yml")
.project_name("my-app")
.volumes() // Remove volumes
.remove_images(RemoveImages::Local) // Remove local images
.remove_orphans() // Remove orphan containers
.timeout(Duration::from_secs(30)) // Graceful shutdown timeout
.execute()
.await;
match down_result {
Ok(_) => println!("Services stopped and cleaned up"),
Err(e) => println!("Cleanup failed (expected without running services): {}", e),
}
println!();
// Example 7: Development workflow patterns
println!("Example 7: Development Workflow Patterns");
println!("----------------------------------");
println!("Common development compose patterns:");
println!();
// Development mode with rebuild
println!("1. Development mode with auto-rebuild:");
println!(" ComposeUpCommand::new()");
println!(" .file(\"docker-compose.dev.yml\")");
println!(" .build() // Always rebuild");
println!(" .force_recreate() // Fresh containers");
println!(" .remove_orphans()");
println!(" .execute()");
println!();
// Production deployment
println!("2. Production deployment:");
println!(" ComposeUpCommand::new()");
println!(" .file(\"docker-compose.prod.yml\")");
println!(" .detach()");
println!(" .wait() // Wait for health checks");
println!(" .execute()");
println!();
// Testing with isolated environment
println!("3. Testing with isolated environment:");
println!(" ComposeUpCommand::new()");
println!(" .project_name(format!(\"test-{{}}\", uuid))");
println!(" .abort_on_container_exit() // Stop when test completes");
println!(" .exit_code_from(\"tests\") // Use test exit code");
println!(" .execute()");
println!();
// Monitoring logs in real-time
println!("4. Real-time log monitoring:");
println!(" ComposeLogsCommand::new()");
println!(" .follow() // Follow log output");
println!(" .timestamps()");
println!(" .service(\"app\")");
println!(" .execute()");
println!("\nDocker Compose example completed!");
println!("\nKey features demonstrated:");
println!(" - Starting services with compose up");
println!(" - Checking service status with compose ps");
println!(" - Viewing logs with compose logs");
println!(" - Scaling services");
println!(" - Selective service management");
println!(" - Clean shutdown with volume/image removal");
println!(" - Development workflow patterns");
Ok(())
}