// 08_nerdctl_web_server.rhai // Demonstrates a complete workflow to set up a web server using nerdctl // Note: This script requires nerdctl to be installed and may need root privileges // Check if nerdctl is installed let nerdctl_exists = which("nerdctl"); if nerdctl_exists == "" { println("Nerdctl is not installed. Please install it first."); // You can use the install_nerdctl.rhai script to install nerdctl // EXIT } println("Starting nerdctl web server workflow..."); // Step 1: Pull the nginx image println("\n=== Pulling nginx:latest image ==="); let pull_result = nerdctl_image_pull("nginx:latest"); if !pull_result.success { println(`Failed to pull nginx image: ${pull_result.stderr}`); // EXIT } println("Successfully pulled nginx:latest image"); // Step 2: Create a custom nginx configuration file println("\n=== Creating custom nginx configuration ==="); let config_content = ` server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; } } `; let config_file = "custom-nginx.conf"; run(`echo "${config_content}" > ${config_file}`); println("Created custom nginx configuration file"); // Step 3: Create a custom index.html file println("\n=== Creating custom index.html ==="); let html_content = `
This page is served by an Nginx container created using the Rhai nerdctl wrapper.
Current time: ${now()}
`; let html_file = "index.html"; run(`echo "${html_content}" > ${html_file}`); println("Created custom index.html file"); // Step 4: Create and run the nginx container with options println("\n=== Creating nginx container ==="); let container_name = "rhai-nginx-demo"; // First, try to remove any existing container with the same name let _ = nerdctl_remove(container_name); let run_options = nerdctl_new_run_options(); run_options.name = container_name; run_options.detach = true; run_options.ports = ["8080:80"]; run_options.snapshotter = "native"; let container = nerdctl_run_with_options("nginx:latest", run_options); if !container.success { println(`Failed to create container: ${container.stderr}`); // EXIT } println(`Successfully created container: ${container_name}`); // Step 5: Copy the custom files to the container println("\n=== Copying custom files to container ==="); let copy_config = nerdctl_copy(config_file, `${container_name}:/etc/nginx/conf.d/default.conf`); if !copy_config.success { println(`Failed to copy config file: ${copy_config.stderr}`); } let copy_html = nerdctl_copy(html_file, `${container_name}:/usr/share/nginx/html/index.html`); if !copy_html.success { println(`Failed to copy HTML file: ${copy_html.stderr}`); } println("Successfully copied custom files to container"); // Step 6: Restart the container to apply changes println("\n=== Restarting container to apply changes ==="); let stop_result = nerdctl_stop(container_name); if !stop_result.success { println(`Failed to stop container: ${stop_result.stderr}`); } let start_result = nerdctl_exec(container_name, "nginx -s reload"); if !start_result.success { println(`Failed to reload nginx: ${start_result.stderr}`); } println("Successfully restarted container"); // Step 7: Commit the container to create a custom image println("\n=== Committing container to create custom image ==="); let image_name = "rhai-nginx-custom:latest"; let commit_result = nerdctl_image_commit(container_name, image_name); if !commit_result.success { println(`Failed to commit container: ${commit_result.stderr}`); } println(`Successfully created custom image: ${image_name}`); // Step 8: Display information about the running container println("\n=== Container Information ==="); println("The nginx web server is now running."); println("You can access it at: http://localhost:8080"); println("Container name: " + container_name); println("Custom image: " + image_name); // Step 9: Clean up (commented out for demonstration purposes) // println("\n=== Cleaning up ==="); // nerdctl_stop(container_name); // nerdctl_remove(container_name); // nerdctl_image_remove(image_name); // delete(config_file); // delete(html_file); println("\nNerdctl web server workflow completed successfully!"); println("The web server is running at http://localhost:8080"); println("To clean up, run the following commands:"); println(` nerdctl stop ${container_name}`); println(` nerdctl rm ${container_name}`); println(` nerdctl rmi ${image_name}`); "Nerdctl web server script completed successfully!"