Organízate con las colecciones
Guarda y clasifica el contenido según tus preferencias.
Packer es una herramienta de código abierto que permite crear imágenes de máquinas virtuales (VMs) idénticas para varias plataformas a partir de una sola configuración de origen. En esta página se explica cómo usar Packer y Cloud Build para crear una imagen de VM que se pueda usar en Compute Engine.
Antes de empezar
En las instrucciones de esta página se da por hecho que conoces Packer. Además, también ocurre lo siguiente:
Cloud Build proporciona una imagen de compilación de la comunidad de Packer que puedes usar para invocar comandos packer en Cloud Build.
Antes de usar este compilador en un archivo de configuración de Cloud Build, debes compilar la imagen y enviarla a Artifact Registry:
Si no especificas [CONFIG_FILE_PATH] y [SOURCE_DIRECTORY] en el comando gcloud builds submit, Cloud Build asume que el archivo de configuración y el código fuente se encuentran en el directorio de trabajo actual.
[[["Es fácil de entender","easyToUnderstand","thumb-up"],["Me ofreció una solución al problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Es difícil de entender","hardToUnderstand","thumb-down"],["La información o el código de muestra no son correctos","incorrectInformationOrSampleCode","thumb-down"],["Me faltan las muestras o la información que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-08-21 (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)."]]