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 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.

What you'll build

In this codelab, you will setup a Kotlin Spring Boot app, build a Docker image, and publish this to Container Registry.

What you'll learn

How to build an optimized Docker image for a Kotlin Spring application and run the containerized app on Google Cloud Platform.

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with building HTML/CSS web apps?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Screenshot from 2016-02-10 12:45:26.png

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

Google Cloud Shell

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.

Activate Google Cloud Shell

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.

Let's run Jib to build a Docker image and publish to Google Container Registry. Set GCP_PROJECT_ID to your Google Cloud Platform project ID below.

$ GCP_PROJECT_ID=< your GCP project ID >
$ ./mvnw com.google.cloud.tools:jib-maven-plugin:1.7.0:build \
    -Dimage=gcr.io/$GCP_PROJECT_ID/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/< your GCP project ID >/kotlin-jib-cloud-run
...
[INFO] BUILD SUCCESS

If you see an error, double-check if GCP_PROJECT_ID is set correctly.

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. Leave the Deployment platform option to the default Cloud Run (fully managed) to have the service fully managed on the Google Cloud Platform. 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.

What's next?

Check out this codelab...

Further reading

Reference docs