Skip to content

Commit 956abe6

Browse files
committed
fix: use SIGKILL and StopCommand in lifecycle example
- SIGKILL immediately terminates container, allowing rm without force - Added second demo showing proper stop -> rm pattern - Print stopped container names from StopResult Fixes #201
1 parent 01bf931 commit 956abe6

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

examples/lifecycle_commands.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Example demonstrating container lifecycle commands including rm, kill, and logs
22

33
use docker_wrapper::command::DockerCommand;
4-
use docker_wrapper::{KillCommand, LogsCommand, RmCommand, RunCommand};
4+
use docker_wrapper::{KillCommand, LogsCommand, RmCommand, RunCommand, StopCommand};
55
use std::time::Duration;
66
use tokio::time::sleep;
77

@@ -42,22 +42,51 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4242
.await?;
4343
println!("Recent logs:\n{}", logs.stdout);
4444

45-
// Send SIGTERM to the container
46-
println!("\nSending SIGTERM to container...");
45+
// Send SIGKILL to the container (demonstrates kill command)
46+
println!("\nSending SIGKILL to container...");
4747
let _kill_result = KillCommand::new(container_name)
48-
.signal("SIGTERM")
48+
.signal("SIGKILL")
4949
.run()
5050
.await?;
51-
println!("Container killed with SIGTERM signal");
52-
53-
// Wait a moment for the container to stop
54-
sleep(Duration::from_secs(1)).await;
51+
println!("Container killed with SIGKILL signal");
5552

5653
// Remove the container
5754
println!("\nRemoving container...");
5855
let _rm_result = RmCommand::new(container_name).run().await?;
5956
println!("Container removed successfully");
6057

58+
// Demonstrate the proper stop -> rm pattern
59+
println!("\n--- Demonstrating stop -> rm pattern ---\n");
60+
61+
// Start another container
62+
println!("Starting another container...");
63+
let run_result = RunCommand::new("alpine:latest")
64+
.name(container_name)
65+
.detach()
66+
.cmd(vec![
67+
"sh".to_string(),
68+
"-c".to_string(),
69+
"while true; do echo 'Running...'; sleep 1; done".to_string(),
70+
])
71+
.execute()
72+
.await?;
73+
println!("Container started with ID: {}", run_result.0);
74+
75+
sleep(Duration::from_secs(2)).await;
76+
77+
// Stop the container gracefully (sends SIGTERM, waits, then SIGKILL)
78+
println!("\nStopping container gracefully...");
79+
let stop_result = StopCommand::new(container_name)
80+
.timeout(5)
81+
.execute()
82+
.await?;
83+
println!("Container stopped: {:?}", stop_result.stopped_containers);
84+
85+
// Now remove it (no force needed since it's stopped)
86+
println!("\nRemoving container...");
87+
let _rm_result = RmCommand::new(container_name).run().await?;
88+
println!("Container removed successfully");
89+
6190
println!("\nLifecycle example completed!");
6291
Ok(())
6392
}

0 commit comments

Comments
 (0)