sal/examples/containers/buildah_run.rhai
2025-06-16 07:30:37 +02:00

45 lines
2.0 KiB
Plaintext

// Now use nerdctl to run a container from the new image
println("\nStarting container from the new image using nerdctl...");
// Create a container using the builder pattern
// Use localhost/ prefix to ensure nerdctl uses the local image
let local_image_name = "localhost/custom-golang-nginx:latest";
println(`Using local image: ${local_image_name}`);
// Import the image from buildah to nerdctl
println("Importing image from buildah to nerdctl...");
process_run("buildah", ["push", "custom-golang-nginx:latest", "docker-daemon:localhost/custom-golang-nginx:latest"]);
let tag_result = nerdctl_image_tag("custom-golang-nginx:latest", local_image_name);
// Tag the image with the localhost prefix for nerdctl compatibility
// println(`Tagging image as ${local_image_name}...`);
// let tag_result = bah_image_tag(final_image_name, local_image_name);
// Print a command to check if the image exists in buildah
println("\nTo verify the image was created with buildah, run:");
println("buildah images");
// Note: If nerdctl cannot find the image, you may need to push it to a registry
// println("\nNote: If nerdctl cannot find the image, you may need to push it to a registry:");
// println("buildah push localhost/custom-golang-nginx:latest docker://localhost:5000/custom-golang-nginx:latest");
// println("nerdctl pull localhost:5000/custom-golang-nginx:latest");
let container = nerdctl_container_from_image("golang-nginx-demo", local_image_name)
.with_detach(true)
.with_port("8081:80") // Map port 80 in the container to 8080 on the host
.with_restart_policy("unless-stopped")
.build();
// Start the container
let start_result = container.start();
println("\nWorkflow completed successfully!");
println("The web server should be running at http://localhost:8081");
println("You can check container logs with: nerdctl logs golang-nginx-demo");
println("To stop the container: nerdctl stop golang-nginx-demo");
println("To remove the container: nerdctl rm golang-nginx-demo");
"Buildah and nerdctl workflow completed successfully!"