Gradle Jib Plugin - Build Docker Container Images with Gradle

What Is Jib?

Jib is Google’s open source Java containerizer that lets you build Java application containers without using a Dockerfile or installing Docker.
Jib is a container image builder that quickly handles every step required to package an application as a container image.
You do not need to write a Dockerfile or install Docker. It works as a Maven and Gradle plugin, so after installing the plugin, you only need to define the target image settings and build configuration to containerize a Java application immediately.

  • Docker build flow Docker build flow

  • Jib build flow Jib build flow

How to Configure and Run Jib

To use Jib, add the related library to build.gradle and configure the container image.

Prerequisites

  • A Java development environment is required.
  • A Docker Hub account is required.
    • If you do not have one, create an account first.

Create a Jib Example Program

First, create a project.

curl https://start.spring.io/starter.tgz \
-d bootVersion=2.7.5 \
-d dependencies=web \
-d baseDir=spring-gradle-jib \
-d groupId=com.devkuma \
-d artifactId=devkuma-gradle-jib \
-d packageName=com.devkuma.hello \
-d applicationName=HelloApplication \
-d packaging=jar \
-d javaVersion=17 \
-d type=gradle-project | tar -xzvf -

Create a simple application as follows.

package com.devkuma.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class HelloApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloApplication.class, args);
	}

	@RequestMapping("/")
	public String hello() {
		return "hello";
	}
}

You can use the generated project above from GitHub.

Add the Jib Library

Add the Jib library to the plugins section of the build.gradle build file.

plugins {

  //... omitted ...

  id 'com.google.cloud.tools.jib' version '3.2.0'
}

Add the Jib Configuration

Add the Jib configuration to the build.gradle file.

The Jib configuration items are as follows.

  • from
    • The most important item that can be configured in from is image.
    • It specifies the base image that the Jib library uses when creating a container image from the application.
    • If nothing is configured, adoptopenjdk:11-jre becomes the default.
  • to
    • The most important items that can be configured in to are image and tags.
    • Here, unlike from, image means the repository where the generated container image will be stored. tags means the tags that will be set on this image.
  • container
    • The container block lets you configure settings such as jvmFlags and environment that are required when the Java application runs as a container image.

Based on the content above, create the Jib configuration as follows.

jib {
    from {
        image = "adoptopenjdk/latest"
    }
    to {
        image = "devkuma/hello-jib"
        tags = ["latest"]
        auth {
          username = 'devkuma'
          password = "xxxx"
        }
    }
    container {
        jvmFlags = ["-Xms128m", "-Xmx128m"]
    }
}
  • from
    • Creates the container image using the adoptopenjdk/latest image as the base image.
  • to
    • Pushes the image to the devkuma/hello-jib repository on Docker Hub with the latest tag and containerizes it.
    • Docker Hub account information was added to auth. (Enter your own account information here.)
  • container
    • At runtime, the two JVM options "-Xms128m" and "-Xmx128m" are added through jvmFlags.

Verify Jib Image Creation

Create the image with the following Jib image creation command.

$ ./gradlew jib

The following is the output from actually running the command.

% ./gradlew jib
Starting a Gradle Daemon, 1 busy and 3 stopped Daemons could not be reused, use --status for details

> Task :jib

Containerizing application to devkuma/gradle-jib, devkuma/gradle-jib...
Base image 'adoptopenjdk' does not use a specific image digest - build may not be reproducible
Using credentials from to.auth for devkuma/gradle-jib
The base image requires auth. Trying again for adoptopenjdk...
Using base image with digest: sha256:3f2982d13b40d8c6d56a3a736c4f6c51a3c99688c2b434a27ed5d1b5f3fbedfb

Container entrypoint set to [java, -Xms128m, -Xmx128m, -cp, @/app/jib-classpath-file, com.devkuma.hello.HelloApplication]

Built and pushed image as devkuma/gradle-jib, devkuma/gradle-jib
Executing tasks:
[==============================] 100.0% complete


BUILD SUCCESSFUL in 58s
3 actionable tasks: 1 executed, 2 up-to-date
user@AD02291831 spring-gradle-jib %

Verify Jib Image Creation and Registration

If you open Docker Hub, you can confirm that the generated image has been registered.

Jib Dockerhub

References