//! Example usage of the nerdctl module //! //! This file demonstrates how to use the nerdctl module to perform //! common container operations like creating containers, running commands, //! and managing images. use sal::virt::nerdctl::{self, NerdctlError}; /// Run a complete nerdctl workflow example pub fn run_nerdctl_example() -> Result<(), NerdctlError> { println!("Starting nerdctl example workflow..."); // Step 1: Pull an image println!("\n=== Pulling nginx:latest image ==="); let pull_result = nerdctl::image_pull("nginx:latest")?; println!("Pull output: {}", pull_result.stdout); // Step 2: Create a container from the image println!("\n=== Creating container from nginx:latest ==="); // Use "native" snapshotter to avoid overlay mount issues let run_result = nerdctl::run("nginx:latest", Some("my-nginx"), true, Some(&["8080:80"]), Some("native"))?; println!("Container created: {}", run_result.stdout.trim()); let container_id = "my-nginx"; // Using the name we specified // Step 3: Execute a command in the container println!("\n=== Installing curl in container ==="); let update_result = nerdctl::exec(container_id, "apt-get update")?; println!("Update output: {}", update_result.stdout); let install_result = nerdctl::exec(container_id, "apt-get install -y curl")?; println!("Installation output: {}", install_result.stdout); // Step 4: Copy a file into the container (assuming nginx.conf exists) println!("\n=== Copying configuration file to container ==="); nerdctl::copy("./nginx.conf", format!("{}:/etc/nginx/nginx.conf", container_id).as_str())?; // Step 5: Commit the container to create a new image println!("\n=== Committing container to create image ==="); let image_name = "my-custom-nginx:latest"; nerdctl::image_commit(container_id, image_name)?; println!("Created image: {}", image_name); // Step 6: Stop and remove the container println!("\n=== Stopping and removing container ==="); nerdctl::stop(container_id)?; nerdctl::remove(container_id)?; println!("Container stopped and removed"); // Step 7: Create a new container from our custom image println!("\n=== Creating container from custom image ==="); // Use "native" snapshotter to avoid overlay mount issues nerdctl::run(image_name, Some("nginx-custom"), true, Some(&["8081:80"]), Some("native"))?; println!("Custom container created"); // Step 8: List images println!("\n=== Listing images ==="); let images_result = nerdctl::images()?; println!("Images: \n{}", images_result.stdout); // Step 9: Clean up (optional in a real workflow) println!("\n=== Cleaning up ==="); nerdctl::stop("nginx-custom")?; nerdctl::remove("nginx-custom")?; nerdctl::image_remove(image_name)?; println!("\nNerdctl example workflow completed successfully!"); Ok(()) } /// Main function to run all examples pub fn run_all_examples() -> Result<(), NerdctlError> { println!("=== NERDCTL MODULE EXAMPLES ===\n"); run_nerdctl_example()?; println!("\nNote that these examples require nerdctl to be installed on your system"); println!("and may require root/sudo privileges depending on your setup."); Ok(()) } fn main() { let _ = run_all_examples().unwrap(); }