Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions examples/lifecycle_commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Example demonstrating container lifecycle commands including rm, kill, and logs

use docker_wrapper::command::DockerCommand;
use docker_wrapper::{KillCommand, LogsCommand, RmCommand, RunCommand};
use docker_wrapper::{KillCommand, LogsCommand, RmCommand, RunCommand, StopCommand};
use std::time::Duration;
use tokio::time::sleep;

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

// Send SIGTERM to the container
println!("\nSending SIGTERM to container...");
// Send SIGKILL to the container (demonstrates kill command)
println!("\nSending SIGKILL to container...");
let _kill_result = KillCommand::new(container_name)
.signal("SIGTERM")
.signal("SIGKILL")
.run()
.await?;
println!("Container killed with SIGTERM signal");

// Wait a moment for the container to stop
sleep(Duration::from_secs(1)).await;
println!("Container killed with SIGKILL signal");

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

// Demonstrate the proper stop -> rm pattern
println!("\n--- Demonstrating stop -> rm pattern ---\n");

// Start another container
println!("Starting another container...");
let run_result = RunCommand::new("alpine:latest")
.name(container_name)
.detach()
.cmd(vec![
"sh".to_string(),
"-c".to_string(),
"while true; do echo 'Running...'; sleep 1; done".to_string(),
])
.execute()
.await?;
println!("Container started with ID: {}", run_result.0);

sleep(Duration::from_secs(2)).await;

// Stop the container gracefully (sends SIGTERM, waits, then SIGKILL)
println!("\nStopping container gracefully...");
let stop_result = StopCommand::new(container_name)
.timeout(5)
.execute()
.await?;
println!("Container stopped: {:?}", stop_result.stopped_containers);

// Now remove it (no force needed since it's stopped)
println!("\nRemoving container...");
let _rm_result = RmCommand::new(container_name).run().await?;
println!("Container removed successfully");

println!("\nLifecycle example completed!");
Ok(())
}