0
0
GcpHow-ToBeginner · 3 min read

How to Create an Image in GCP: Step-by-Step Guide

To create an image in GCP, use the gcloud compute images create command or the Google Cloud Console to capture a snapshot of a VM disk. This image can then be used to launch new VM instances with the same setup.
📐

Syntax

The basic syntax to create an image from a disk in GCP using the command line is:

  • gcloud compute images create IMAGE_NAME --source-disk=DISK_NAME --source-disk-zone=ZONE

Where:

  • IMAGE_NAME is the name you want to give your new image.
  • DISK_NAME is the name of the existing disk you want to create the image from.
  • ZONE is the zone where the disk is located.
bash
gcloud compute images create IMAGE_NAME --source-disk=DISK_NAME --source-disk-zone=ZONE
💻

Example

This example creates a new image named my-custom-image from a disk called my-vm-disk located in the us-central1-a zone.

bash
gcloud compute images create my-custom-image --source-disk=my-vm-disk --source-disk-zone=us-central1-a
Output
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/images/my-custom-image].
⚠️

Common Pitfalls

Common mistakes when creating images in GCP include:

  • Using the wrong disk name or zone, which causes the command to fail.
  • Trying to create an image from a disk that is currently attached and in use without stopping the VM, which can lead to inconsistent images.
  • Not having the required permissions to create images in the project.

Always verify the disk name and zone with gcloud compute disks list and stop the VM if possible before creating the image.

bash
## Wrong way (disk name typo)
gcloud compute images create my-image --source-disk=my-vm-disk-typo --source-disk-zone=us-central1-a

## Right way
gcloud compute images create my-image --source-disk=my-vm-disk --source-disk-zone=us-central1-a
📊

Quick Reference

CommandDescription
gcloud compute images create IMAGE_NAME --source-disk=DISK_NAME --source-disk-zone=ZONECreate an image from an existing disk
gcloud compute disks listList all disks to find disk names and zones
gcloud compute instances stop INSTANCE_NAME --zone=ZONEStop a VM instance before creating an image
gcloud compute images listList all available images in the project

Key Takeaways

Use the gcloud command with correct disk name and zone to create an image.
Stop the VM before creating an image to ensure data consistency.
Verify permissions to create images in your GCP project.
Use images to launch new VMs with the same configuration easily.