Spring Boot is one of the most popular frameworks for building Java-based Microservices. With Google Cloud Run, you can deploy your Spring boot application in a server less, fully managed environment without worrying about provisioning servers, scaling or infrastructure management.
In this article, I will walk you step-by-step through deploying a spring Boot application to Cloud Run.
GitHub:
https://github.com/lalamanil/IntegratingAccidentPredictionModelOfVideo.git
Prerequisite:
A Google Cloud account with billing enabled
Google Cloud SDK installed locally
A simple Spring Boot application (Source Code)
Step 1: Verify gcloud Installation and Project Configuration
First, make sure the Google Cloud CLI (gcloud) is installed and accessible:
gcloud - version
Then Check the currently active project
gcloud config get-value project
If you need to set a project
gcloud config set project
Finally, set the default compute region and zone (Optional but recommended)
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-a
STEP:2 Verify Required APIS are Enabled
Before deploying, ensure the required services are enabled in your project.
Check enabled services
gcloud services list --enabled
If any are missing, enable them using below commands:
gcloud services enable artifactregistry.googleapis.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable compute.googleapis.com
STEP:3 Create an Artifact Registry Repository
Artifact Registry is the recommended place to store your Docker Image
gcloud artifacts repositories list
Create the repository:
gcloud artifacts repositories create springboot-docker-repo --repository-format=docker --location=us-central1 --description="Docker repo for Spring Boot images"
Verify:
gcloud artifacts repositories list
STEP:4 Create a multi stage Docker File in Spring boot Application
In the root of your Spring Boot project, add a Dockerfile:
STEP:5 Build and Push the Docker image Using Cloud Build
Instead of building locally with Docker, you can use **Cloud Build **to build and push the image directly to Artifact Registry.
Set the directory path to root folder of your spring boot application
Checking Artifact repository if any images are already present.
gcloud artifacts docker images list us-central1-docker.pkg.dev/videoanalyzer-455321/springboot-docker-repo
Build & Push:
gcloud builds submit --tag=us-central1-docker.pkg.dev/videoanalyzer-455321/springboot-docker-repo/cloudrunspringbootdeploy:v1
This command will:
- Build the docker image in Google Cloud
- Tag it with the specified Artifact Registry path
- Push it automatically to your Artifact Registry repository
** Verify:**
Image is created and pushed to artifact registry
STEP:6 Deploy to Cloud Run
Delpoy the container directly from Artifact Registry
*gcloud run deploy springboot-service --image=us-central1-docker.pkg.dev/videoanalyzer-455321/springboot-docker-repo/cloudrunspringbootdeploy:v1 --region=us-central1 --allow-unauthenticated *
Verify:
There is an endpoint /healthCheck defined in Spring boot application as shown in below screen shot
Able to hit rest endpoint via browser
Below are the logs at Cloud Run in Google cloud console.
Top comments (0)