Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Packer ist ein Open-Source-Tool zum Erstellen identischer VM-Images (virtuelle Maschine) für mehrere Plattformen aus einer einzelnen Quellkonfiguration. Auf dieser Seite wird erläutert, wie Sie mithilfe von Packer und Cloud Build ein VM-Image zur Verwendung in Compute Engine erstellen.
Hinweise
Die Anleitung auf dieser Seite setzt voraus, dass Sie mit Packer vertraut sind. Außerdem gilt:
Halten Sie Ihren Quellcode bereit, einschließlich der Packer-Vorlage.
Wenn Sie die gcloud-Befehle auf dieser Seite verwenden möchten, installieren Sie die Google Cloud CLI.
Cloud Build bietet ein Builder-Image der Packer-Community, mit dem Sie in Cloud Build packer-Befehle aufrufen können.
Bevor Sie diesen Builder in einer Cloud Build-Konfigurationsdatei verwenden können, müssen Sie erst das Image erstellen und an Artifact Registry übertragen:
Wenn Sie [CONFIG_FILE_PATH] und [SOURCE_DIRECTORY] im Befehl gcloud builds submit nicht angeben, geht Cloud Build davon aus, dass sich die Konfigurationsdatei und der Quellcode im aktuellen Arbeitsverzeichnis befinden.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-18 (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)."]]