Search code examples
google-cloud-platformgoogle-apigoogle-cloud-compute-engine

How do I deploy a container to a GCP VM using the Java API?


I am creating GCP VMs using the programmable Java APIs interface.

Maven dependency:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-compute</artifactId>
    <version>1.21.0</version>
</dependency>

Java code:

Instance instanceResource = Instance.newBuilder()
                .setName(instanceName)
                .setMachineType(MACHINE_TYPE)
                .addDisks(disk)
                 // etc (...)
                .build();

When I create a new VM using the GCP user interface, I see an option "Deploy a container image to this VM instance" in the "Container" section of the main screen.

enter image description here

How can I use that feature and deploy a container to the VM using the Java code?

In other words, how do I use the gcloud compute instances create-with-container command from Java?


Solution

  • As per the GCP official document Deploying containers on VMs and MIGs Limitation,

    You can only use this feature through the Google Cloud console or the Google Cloud CLI, not the API.