Deploying a Java Application to Google Cloud Run in VS Code

This guide shows you how to create a Java application, containerize, and deploy it to Google Cloud Run from within VS Code using the Graal Development Kit for Micronaut Extension Pack.

Google Cloud Run enables you to deploy containerized Java applications on a fully managed platform.

Prerequisites #

  • A Google Cloud Platform (GCP) account. Create an account at
  • The Google Cloud CLI to interact with Google Cloud services from the terminal. (The CLI is part of the Google Cloud SDK; for more information, see Cloud SDK.)
  • A Docker-API compatible container runtime such as Rancher Desktop or Docker.

Note: This guide uses paid services; you may need to enable billing in Google Cloud to complete some steps in this guide.

1. Install Extensions #

Follow this guide to install the Graal Development Kit for Micronaut Extension Pack and its dependencies in VS Code.

2. Create a Java Application Using Graal Development Kit for Micronaut Launcher #

Create a Java application, using the Graal Development Kit for Micronaut Launcher, that you will later containerize and deploy to Google Cloud Run.

  1. Go to View, Command Palette, search for “Graal Dev Kit”, and invoke the Graal Dev Kit: Create New Project quick action:

    Create New Project Action

  2. Follow the command prompts to generate a new Maven or Gradle project. For example, to create a simple Java application:
    • Pick Micronaut version: Default
    • Pick application type: Application
    • Pick project Java: GraalVM or Other Java
    • Provide a project name: cloudrun-demo
    • Provide base package: com.example
    • Pick project services: none. Click OK to confirm
    • Pick build tool: Gradle (Groovy) or Maven
    • Pick test framework: JUnit
    • Pick cloud platform: GCP. Click OK to confirm
    • Select the destination directory on your local disk and whether to open the project in a new window or add it to the current workspace. The wizard creates a directory named cloudrun-demo containing an application in a package named com.example.
  3. (Required only for macOS AArch64.) Add the following GCP module configuration:

    tasks.named("dockerfile") {
             instruction """FROM --platform=linux/amd64 eclipse-temurin:17-jre-focal
                 WORKDIR /home/app
                 COPY layers/libs /home/app/libs
                 COPY layers/classes /home/app/classes
                 COPY layers/resources /home/app/resources
                 COPY layers/application.jar /home/app/application.jar

    This configuration that Gradle/Maven tools build a container image using the openjdk:17-slim base image (it defaults to openjdk:17-alpine which is not multiplatform).

  4. Open a new Terminal window in VS Code (Terminal > New Terminal). Build the project and run the application from a JAR file:

     ./gradlew :gcp:run

3. Build a Container Image and Push to a Container Repository #

  1. Build a container image for your Java application. In your project’s root directory, run:

     ./gradlew dockerBuild
  2. Use the docker images command to confirm that the container image was successful created. The gdk-to-gke-gcp container image is the one you will deploy to GKE.

     docker images
  3. Tag your Docker image with Google Cloud’s registry address using the command:

     docker tag <docker image ID or name><PROJECT ID>/<ImageName>
  4. Save your credentials securely in the $HOME/.docker/config.json file using the Google Cloud CLI (only required for the first time):

     gcloud auth configure-docker
  5. Authenticate Docker with the Google Cloud Platform from the terminal:

     gcloud auth login

    It will enable you to push and pull images from Google Cloud.

  6. Push your container image to the Google Cloud Artifact Registry:

     docker push<Project ID>/<ImageName>
  7. Verify the successful creation of your container image in the Google Cloud Artifact Registry. Login to the console and check the creation of the repository.

4. Deploy to Google Cloud Run #

There are two ways to deploy a containerized application to Google Cloud Run: using the Google Cloud CLI or the Google Cloud console. Both are be described below.

Google Cloud CLI #

Run the following command to deploy your application to Google Cloud Run using the Google Cloud CLI. Specify a name for the service:

gcloud run deploy <service name> --image<ProjectID>/<ImageName>

You will be asked to provide the number of the region. Select “Yes” to allow unauthenticated requests, if necessary. Once completed, the application will be accessible from the Service URL printed to the console.

Google Cloud Console #

Perform the following steps to deploy your application to Google Cloud Run from the console:

  1. Login to the Google Cloud console and open the menu.
  2. Navigate to Cloud Run.
  3. Click Create service.
  4. Check “Deploy one revision from an existing container image”.
  5. Select the Artifact Registry (or Container registry) where your container repository was created.
  6. Select your container image from the repository and confirm the selection.
  7. For authentication, select “Allow unauthenticated invocations.”
  8. Finally, click Create.

Once created, open the Service URL in a browser to check that your application is successfully deployed and running on Google Cloud Platform.

Summary #

This guide demonstrated how to easily deploy your container-based workload to Google Cloud Run services from within VS Code using the Graal Development Kit for Micronaut Extension Pack.