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.

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

Codelab-at-a-conference setup

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 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" :

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

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 \
    -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
$ nano 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

class Controller {
  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

Let's run Jib to build a Docker image and publish to Google Container Registry.

$ ./mvnw \$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

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

What's next?

Check out this codelab...

Further reading

Reference docs