// 07_nerdctl_operations.rhai // Demonstrates container operations using SAL's nerdctl integration // Note: This script requires nerdctl to be installed and may need root privileges // Check if nerdctl is installed let nerdctl_exists = which("nerdctl"); println(`Nerdctl exists: ${nerdctl_exists}`); // List available images (only if nerdctl is installed) println("Listing available container images:"); if nerdctl_exists == "" { println("Nerdctl is not installed. Please install it first."); // You can use the install_nerdctl.rhai script to install nerdctl // EXIT } // List images let images_result = nerdctl_images(); println(`Images result: success=${images_result.success}, code=${images_result.code}`); println(`Images output:\n${images_result.stdout}`); // Pull an image if needed println("\nPulling alpine:latest image:"); let pull_result = nerdctl_image_pull("alpine:latest"); println(`Pull result: success=${pull_result.success}, code=${pull_result.code}`); println(`Pull output: ${pull_result.stdout}`); // Create a container using the simple run function println("\nCreating a container from alpine image:"); let container_name = "rhai-nerdctl-test"; let container = nerdctl_run("alpine:latest", container_name); println(`Container result: success=${container.success}, code=${container.code}`); println(`Container stdout: "${container.stdout}"`); println(`Container stderr: "${container.stderr}"`); // Run a command in the container println("\nRunning a command in the container:"); let run_result = nerdctl_exec(container_name, "echo 'Hello from container'"); println(`Command output: ${run_result.stdout}`); // Create a test file and copy it to the container println("\nAdding a file to the container:"); let test_file = "test_file.txt"; run(`echo "Test content" > ${test_file}`); let copy_result = nerdctl_copy(test_file, `${container_name}:/`); println(`Copy result: ${copy_result.success}`); // Commit the container to create a new image println("\nCommitting the container to create a new image:"); let image_name = "my-custom-alpine:latest"; let commit_result = nerdctl_image_commit(container_name, image_name); println(`Commit result: ${commit_result.success}`); // Stop and remove the container println("\nStopping the container:"); let stop_result = nerdctl_stop(container_name); println(`Stop result: ${stop_result.success}`); println("\nRemoving the container:"); let remove_result = nerdctl_remove(container_name); println(`Remove result: ${remove_result.success}`); // Clean up the test file delete(test_file); // Demonstrate run options println("\nDemonstrating run options:"); let run_options = nerdctl_new_run_options(); run_options.name = "rhai-nerdctl-options-test"; run_options.detach = true; run_options.ports = ["8080:80"]; run_options.snapshotter = "native"; println("Run options configured:"); println(` - Name: ${run_options.name}`); println(` - Detach: ${run_options.detach}`); println(` - Ports: ${run_options.ports}`); println(` - Snapshotter: ${run_options.snapshotter}`); // Create a container with options println("\nCreating a container with options:"); let container_with_options = nerdctl_run_with_options("alpine:latest", run_options); println(`Container with options result: ${container_with_options.success}`); // Clean up the container with options println("\nCleaning up the container with options:"); nerdctl_stop("rhai-nerdctl-options-test"); nerdctl_remove("rhai-nerdctl-options-test"); // List all containers (including stopped ones) println("\nListing all containers:"); let list_result = nerdctl_list(true); println(`List result: ${list_result.stdout}`); // Remove the custom image println("\nRemoving the custom image:"); let image_remove_result = nerdctl_image_remove(image_name); println(`Image remove result: ${image_remove_result.success}`); "Nerdctl operations script completed successfully!"