Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
O Packer é uma ferramenta de código aberto para criar imagens de máquinas virtuais (VMs, na sigla em inglês) idênticas para várias plataformas a partir de uma única configuração de origem. Nesta página, explicamos como usar o Packer e o Cloud Build para criar uma imagem de VM para uso no Compute Engine.
Antes de começar
Nas instruções desta página, pressupomos que você está familiarizado com Packer. Além disso:
O Cloud Build oferece uma imagem do builder da comunidade do Packer que pode ser usada para invocar comandos packer no Cloud Build.
Antes de usar esse builder em um arquivo de configuração do Cloud Build, é necessário criar a imagem e enviá-la para o Artifact Registry:
Se você não especificar [CONFIG_FILE_PATH] e [SOURCE_DIRECTORY] no comando gcloud builds submit, o Cloud Build presumirá que o arquivo de configuração e o código-fonte estão no diretório de trabalho atual.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-09-01 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)."]]