Google provides a powerful image build tool with which you can easily build and publish an optimized Docker container image for Java applications in no time without Docker or Dockerfile. Google Cloud Platform (GCP) also brings serverless to containers with Cloud Run, a managed compute platform that automatically scales your stateless containers. In this codelab, you will see how easy it is to containerize your Kotlin app, publish to Google Container Registry, and run the image on Google Cloud Platform in a seamless manner!
This codelab walks you through setting up a simple application in Kotlin which demonstrates using GCP services and tools including: Jib, Container Registry, and Cloud Run.
In this codelab, you will setup a Kotlin Spring Boot app, build a Docker image, and publish this to Container Registry.
How to build an optimized Docker image for a Kotlin Spring application and run the containerized app on Google Cloud Platform.
If you see a "request account button" at the top of the main Codelabs window, click it to obtain a temporary account. Otherwise ask one of the staff for a coupon with username/password.
These temporary accounts have existing projects that are set up with billing so that there are no costs associated for you with running this codelab.
Note that all these accounts will be disabled soon after the codelab is over.
Use these credentials to log into the machine or to open a new Google Cloud Console window https://console.cloud.google.com/. Accept the new account Terms of Service and any updates to Terms of Service.
Here's what you should see once logged in:
When presented with this console landing page, please select the only project available. Alternatively, from the console home page, click on "Select a Project" :
While Google Cloud can be operated remotely from your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.
From the GCP Console click the Cloud Shell icon on the top right toolbar:
Then click "Start Cloud Shell":
It should only take a few moments to provision and connect to the environment:
This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.
Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.
Run the following command in Cloud Shell to confirm that you are authenticated:
gcloud auth list
Command output
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
gcloud config list project
Command output
[core] project = <PROJECT_ID>
If it is not, you can set it with this command:
gcloud config set project <PROJECT_ID>
Command output
Updated property [core/project].
Let's generate a new Spring Boot app with Spring Initializr:
$ curl https://start.spring.io/starter.tgz \ -d language=kotlin \ -d dependencies=web \ -d baseDir=kotlin-jib-cloud-run | tar -xzvf -
Note that the Initializr will automatically add the spring-boot-starter-web
to our dependencies in the pom.xml
of the template app.
Change to the directory of the template app:
$ cd kotlin-jib-cloud-run
Build and run the app using Maven:
$ ./mvnw -DskipTests spring-boot:run
Once started, the app will start listening on port 8080. Click on the Web Preview iconin the Cloud Shell toolbar and choose Preview on port 8080 to access the app.
You should get back a 404 response because the app doesn't do anything useful yet. Stop the app with Ctrl+C
.
Create the following Controller
class in the demo package:
$ vi src/main/kotlin/com/example/demo/Controller.kt or $ nano src/main/kotlin/com/example/demo/Controller.kt
src/main/kotlin/com/example/demo/Controller.kt
package com.example.demo
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class Controller {
@GetMapping("/")
fun saySomething(): String {
return "Kotlin app on Cloud Run, containerized by Jib!"
}
}
Rebuild and run the app:
$ ./mvnw spring-boot:run
Let's check the app again using Web Preview (). This time, you should see the message "
Kotlin app on Cloud Run, containerized by Jib!
". Stop the app with Ctrl+C
.
With Jib, you can containerize your app in an optimized way without Docker and publish to any container registry.
Before proceeding, you need to activate the Container Registry API. This only needs to be done once per project to make the API accessible.
$ gcloud services enable containerregistry.googleapis.com
Let's run Jib to build a Docker image and publish to Google Container Registry.
$ ./mvnw com.google.cloud.tools:jib-maven-plugin:1.8.0:build \ -Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/kotlin-jib-cloud-run
Eventually, you will see the following message that the app is containerized and pushed to your Container Registry.
[INFO] Built and pushed image as gcr.io/PROJECT_ID/kotlin-jib-cloud-run ... [INFO] BUILD SUCCESS
If you see an error, double-check if $GOOGLE_CLOUD_PROJECT
is set correctly to your Google Cloud project ID (PROJECT_ID
).
Before moving on, let's check if the image is successfully published. Go back to the Cloud Console (https://console.cloud.google.com/), click on the navigation menu button, and select Container Registry on the left panel.
You will see that your image is successfully published.
Cloud Run brings serverless to containers, automatically scaling your stateless containers.
Click on the navigation menu button again and select Cloud Run.
If this is your first time accessing Cloud Run, you will see the following page for one-time setup. Click on Start Using Cloud Run if it shows up.
On the Cloud Run page, click on Create Service.
In the next screen, click the Select button for the Source field. The Source field is for the image we want to run on Cloud Run.
The popup will show the image you built previously. Select the image and click Continue.
We are just a couple clicks away to deploy the app now. Under Deployment platform, choose Cloud Run (fully managed) to have the service fully managed on the Google Cloud Platform. Choose a region appropriate for your location, select Allow unauthenticated invocations, and click on Create. That's it!
When the image is fully deployed, the Cloud Run page will display a URL to access the app. Let's check it out!
In the end, you will see the message we expect from the app.
Kotlin app on Cloud Run, containerized by Jib!
That's it! In the future, if you need to deploy new app versions, you can do so by clicking on Deploy New Revision on the page.
To clean up your environment, you need to delete the deployed app on Cloud Run and the published image on Container Registry. Go to the Cloud Run page, select the app, and click on Delete.
Similarly, go to the Container Registry page and delete the image.
Congratulations, you've successfully containerized your Kotlin Spring app and deployed to Cloud Run!
Using Jib, you built an optimized container image without requiring Docker installed or writing Dockerfile and published it to Container Registry. Jib optimizes image construction, so anyone with no in-depth Docker knowledge can containerize Java apps fast and efficiently. Then with a few clicks, you deployed the app on Cloud Run to start serving in no time.
Check out this codelab...