0%

Jib, a tool for containerizing your Java app

Objective

Introduce what jib is, how to use jib to build docker image of Spring Boot app

Requirement & Prerequisites

Machine

  • Java 8+
  • Docker runtime
  • Docker hub account or other registry sapce

Human

  • Basic Spring Boot understanding
  • Basic Docker immae, container knowledge

Introduction

Jib is

  • a tool to help you containerize your java app without having docker deamon and Dockerfile
  • an open source project by Google, jib github repo
  • a plugin supported for both gradle and maven

What is the difference from common Dockerfile build flow?

a blog post from GCP Blog describes very clearly (image from: Introducing Jib — build Java Docker images better)
— highly recommend to read this blog post if you want learn jib more —

Demo with Spring Boot Greeting App

clone this repo beforehand: Spring Boot Guides - Building a RESTful Web Service

add jib plugins build.gradle

1
2
3
4
5
6
7
8
9
plugins {  
id 'org.springframework.boot' version '2.2.0.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
// add plugin
id 'com.google.cloud.tools.jib' version '2.2.0'
}

// other dependencires, scripts ...

Build image without dokcer deamon (authentication of image registry is necessary)

Default is docker hub registry.
But Azure Container Registry (ACR) , Google Container Registry (GCR) and Amazon Elastic Container Registry (ECR) are all supported

1
2
3
4
5
6
7
8
# login  
$ docker login
Username: ${docker hub username}
Password:
Login Succeeded

# build and push image automatically
$ ./gradlew jib --image=${docker hub username}/spring-service-by-jib

Build image with dokcer deamon, and run the container

1
2
3
4
5
6
7
8
9
10
# build image  
$ ./gradlew jibDockerBuild

# check images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rest-service 0.0.1-SNAPSHOT fc64280956f4 50 years ago 143MB

# run the container
$ docker run -p 8080:8080 -p 8080:8080 rest-service:0.0.1-SNAPSHOT

make a request by curl command

1
2
3
4
5
$ curl -s http://localhost:8080/greeting  
{"id":6,"content":"Hello, World!"}

$ curl -s http://localhost:8080/greeting\\?name=john
{"id":7,"content":"Hello, john!"}

Yeah!!! Building a java app image is so easy!!!

Summary

Through this article, we can see how easy it is to build an java app image without Dockerfile or Docker deamon.

The reason I like jib is, I don’t have to install docker on the build server, like Jenkins, and I still can build an image and push to the target image registry.

In our team, we have already lots of build jar file projects on Jenkins server, and I don’t want to ruin it or take more time to install it. That’s why I seek for the solution and use jib instead.