How to run Spring Boot on Google Cloud Run ?

ADAD
2 min read

Step by step guide to deploying spring boot application on Google Cloud Run

Introduction:

Cloud Run, a fully managed compute platform provided by Google Cloud, allows developers to deploy and run containerized applications effortlessly. In this article, we will explore the process of deploying Spring Boot applications on Cloud Run, taking advantage of its scalability, flexibility, and ease of use.

Prerequisites:

Before diving into deploying a Spring Boot application on Cloud Run, ensure you have the following prerequisites in place:

  1. A Google Cloud Platform (GCP) account.

  2. The Google Cloud SDK installed on your local machine.

  3. Docker installed on your local machine.

Getting Started:

  1. Create a Spring Boot Application: If you don't have a Spring Boot application, create one using Spring Initializr or your preferred method.

  2. Dockerize the Spring Boot Application: Build a Docker image for your Spring Boot application. Create a Dockerfile in the root of your project to describe the container image. For example:

     DockerfileCopy codeFROM adoptopenjdk/openjdk11:alpine-jre
     ARG JAR_FILE=target/*.jar
     COPY ${JAR_FILE} app.jar
     ENTRYPOINT ["java", "-jar", "/app.jar"]
    

    Build the Docker image using the following command:

     bashCopy codedocker build -t your-image-name .
    
  3. Push the Docker Image to Container Registry: Upload your Docker image to Google Container Registry (GCR) or another container registry of your choice. Make sure to tag your image appropriately.

     bashCopy codedocker tag your-image-name gcr.io/your-project-id/your-image-name
     docker push gcr.io/your-project-id/your-image-name
    

Deploying on Cloud Run:

  1. Enable Cloud Run API: Make sure the Cloud Run API is enabled for your GCP project. You can enable it through the Google Cloud Console or using the following gcloud command:

     bashCopy codegcloud services enable run.googleapis.com
    
  2. Deploy the Spring Boot Application: Use the following command to deploy your Spring Boot application on Cloud Run:

     bashCopy codegcloud run deploy --image gcr.io/your-project-id/your-image-name --platform managed
    

    Follow the prompts to specify the region, service name, and allow unauthenticated invocations.

  3. Access Your Deployed Application: Once the deployment is complete, you will receive a URL to access your Spring Boot application on Cloud Run. Open the provided URL in your web browser to test the deployment.

Conclusion:

Deploying Spring Boot applications on Google Cloud Run is a straightforward process that takes advantage of the serverless architecture and scalability offered by the platform. By following the steps outlined in this guide, you can quickly containerize your Spring Boot application and deploy it on Cloud Run, allowing you to focus on building and scaling your application without the hassle of managing infrastructure.

0
Subscribe to my newsletter

Read articles from AD directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

AD
AD