Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Packer è uno strumento open source per creare immagini di macchine virtuali (VM) identiche per più piattaforme da una singola configurazione di origine. Questa pagina spiega come utilizzare Packer e Cloud Build
per creare un'immagine VM da utilizzare su Compute Engine.
Prima di iniziare
Le istruzioni riportate in questa pagina presuppongono che tu abbia familiarità con Packer. Inoltre:
Tieni a portata di mano il codice sorgente, incluso il modello Packer.
Se vuoi utilizzare i comandi gcloud in questa pagina, installa
Google Cloud CLI.
Cloud Build fornisce un'immagine del builder della community Packer che puoi utilizzare per richiamare i comandi packer in Cloud Build.
Prima di utilizzare questo builder in un file di configurazione di Cloud Build, devi creare l'immagine ed eseguirne il push su Artifact Registry:
Se non specifichi un [CONFIG_FILE_PATH] e un [SOURCE_DIRECTORY] nel comando gcloud builds submit, Cloud Build presuppone che il file di configurazione e il codice sorgente si trovino nella directory di lavoro corrente.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003ePacker is used with Cloud Build to create identical VM images for multiple platforms from a single source configuration.\u003c/p\u003e\n"],["\u003cp\u003eYou must enable several APIs and grant specific IAM permissions to your build service account, including Compute Engine Instance Admin and Artifact Registry Writer, to use Packer with Cloud Build.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the Packer community builder image, it must be built and pushed to the Container Registry, but be aware that Container Registry is deprecated and will be shut down by March 18, 2025.\u003c/p\u003e\n"],["\u003cp\u003eTo use the Packer builder, you will need a \u003ccode\u003epacker.json\u003c/code\u003e file, and a \u003ccode\u003ecloudbuild.yaml\u003c/code\u003e or \u003ccode\u003ecloudbuild.json\u003c/code\u003e configuration file specifying the \u003ccode\u003epacker build\u003c/code\u003e command and associated variables, including project ID, image name, family, and zone.\u003c/p\u003e\n"],["\u003cp\u003eYou can initiate the build process using the \u003ccode\u003egcloud builds submit\u003c/code\u003e command, which references the configuration file, the source code location, and the target region for the build.\u003c/p\u003e\n"]]],[],null,["# Building VM images using Packer\n\n[Packer](https://www.packer.io/) is an open\nsource tool for creating identical Virtual Machine (VM) images for multiple platforms from a\nsingle source configuration. This page explains how to use Packer and Cloud Build\nto create a VM image for use on Compute Engine.\n\nBefore you begin\n----------------\n\nThe instructions on this page assume that you are familiar with `Packer`. In addition:\n\n- Have your source code including the [Packer template](https://www.packer.io/docs/templates) handy.\n- If you want to use the `gcloud` commands in this page, install the [Google Cloud CLI](https://cloud.google.com/sdk).\n- Enable the following APIs:\n\n gcloud services enable sourcerepo.googleapis.com\n gcloud services enable compute.googleapis.com\n gcloud services enable servicemanagement.googleapis.com\n gcloud services enable storage-api.googleapis.com\n\n### Required IAM permissions\n\n- To use Packer with Cloud Build, grant the [Compute Engine\n Instance Admin role (`roles/compute.instanceAdmin.v1`)](/artifact-registry/docs/access-control#grant)\n to your build service account.\n\n- To store built images in Artifact Registry, grant the [Artifact Registry Writer\n (`roles/artifactregistry.writer`) role](/artifact-registry/docs/access-control#grant)\n to your build service account.\n\nCreating a Packer builder image\n-------------------------------\n\nCloud Build provides a\n[Packer community builder image](https://github.com/GoogleCloudPlatform/cloud-builders-community/tree/master/packer)\nthat you can use to invoke `packer` commands in Cloud Build.\nBefore using this builder in a Cloud Build config file, you must build\nthe image and push it to Artifact Registry:\n\n1. Clone the [cloud-builders-community](https://github.com/GoogleCloudPlatform/cloud-builders-community) repository:\n\n git clone https://github.com/GoogleCloudPlatform/cloud-builders-community.git\n\n2. Navigate to the Packer builder image:\n\n cd cloud-builders-community/packer\n\n3. Submit the builder to your project:\n\n gcloud builds submit .\n\nUsing the Packer builder\n------------------------\n\n1. Ensure that you have your [packer.json](https://www.packer.io/docs/templates) file along with your source code.\n\n2. In your project root directory, create a [build config file](/build/docs/build-config)\n named `cloudbuild.yaml` or `cloudbuild.json`.\n\n3. In your build config file, add a build step to invoke the `packer build` command:\n\n ### YAML\n\n steps:\n - name: 'gcr.io/[PROJECT_ID]/packer'\n args:\n - build\n - -var\n - image_name=[IMAGE_NAME]\n - -var\n - project_id=[PROJECT_ID]\n - -var\n - image_family=[IMAGE_FAMILY]\n - -var\n - image_zone=[IMAGE_ZONE]\n - packer.json\n\n ### JSON\n\n {\n \"steps\": [\n {\n \"name\": \"gcr.io/[PROJECT_ID]/packer\",\n \"args\": [\n \"build\",\n \"-var\",\n \"image_name=[IMAGE_NAME]\",\n \"-var\",\n \"project_id=[PROJECT_ID]\",\n \"-var\",\n \"image_family=[IMAGE_FAMILY]\",\n \"-var\",\n \"image_zone=[IMAGE_ZONE]\",\n \"packer.json\"\n ]\n }\n ]\n }\n\n Where:\n - `[PROJECT_ID]` is your Google Cloud project ID.\n - `[IMAGE_NAME]` is the name of the VM image you're building.\n - `[IMAGE_FAMILY]` is the [image family](/compute/docs/images#image_families) of the VM image.\n - `[IMAGE_ZONE]` is the [image zone](/compute/docs/regions-zones).\n4. Start the build using the build config file:\n\n gcloud builds submit --region=[REGION] --config [CONFIG_FILE_PATH] [SOURCE_DIRECTORY]\n\n Where:\n - `[CONFIG_FILE_PATH]` is the path to the build config file.\n - `[SOURCE_DIRECTORY]` is the path or URL to the source code.\n - `[REGION]` is one of the [supported build regions](/build/docs/locations).\n\n If you don't specify a `[CONFIG_FILE_PATH]` and `[SOURCE_DIRECTORY]` in the\n `gcloud builds submit` command, Cloud Build assumes that the config\n file and the source code are in the current working directory.\n\nOnce the images are built, you can view them in the [Compute Engine Image page](https://console.cloud.google.com/compute/images)\nin the Google Cloud console.\n\nWhat's next\n-----------\n\n- Learn how to [build containers](/build/docs/building/build-containers).\n- Learn how to [build `Go` projects](/build/docs/building/build-go).\n- Learn how to [troubleshoot build errors](/build/docs/troubleshooting)."]]