Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Packer est un outil Open Source permettant de créer des images de machines virtuelles (VM) identiques pour plusieurs plates-formes à partir d'une même configuration source. Cette page explique comment utiliser Packer et Cloud Build pour créer une image de VM à utiliser sur Compute Engine.
Avant de commencer
Les instructions de cette page partent du principe que vous connaissez bien Packer. Notez en outre les points suivants :
Gardez à portée de main votre code source, y compris le modèle Packer.
Si vous souhaitez utiliser les commandes gcloud sur cette page, installez Google Cloud CLI.
Cloud Build fournit une image de compilateur Packer générée par la communauté que vous pouvez utiliser pour appeler des commandes packer dans Cloud Build.
Avant d'utiliser ce compilateur dans un fichier de configuration Cloud Build, vous devez générer l'image et l'importer dans Artifact Registry :
Si vous ne spécifiez pas les éléments [CONFIG_FILE_PATH] et [SOURCE_DIRECTORY] dans la commande gcloud builds submit, Cloud Build considère que le fichier de configuration et le code source se trouvent dans le répertoire de travail actuel.
Une fois les images générées, vous pouvez les consulter sur la page Image Compute Engine de la console Google Cloud .
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]