Jib is an open-source tool which can help you to quickly build an efficient container/docker image for Spring Boot and Java applications without the need of a Dockerfile or even a Docker daemon

Under the hood, Jib separates your application into multiple layers, splitting dependencies from classes, only build and deploy the layers that changed, so you can build a docker image faster than a traditional single image layer jar

Beside Maven, Jib also provide a Gradle plugin to help you integrate the build container image into the project build process

This tutorial will show you the steps to build a docker image for a sample Spring Boot application with Jib

Let's get started!

What you will need

Your local computer should have installed JDK 1.8+ and the JAVA_HOME environment variable was set properly

Create a sample Spring Boot application

Create a new Spring Boot application with Spring Initializr via web UI or a command-line tool such as cURL or HTTPie, you can find the guide at here

Example with the cURL command-line

curl https://start.spring.io/starter.zip \  
    -d dependencies=webflux,actuator \
    -d javaVersion=1.8 \
    -d packageName=com.hellokoding.tutorials \
    -d groupId=com.hellokoding.tutorials \
    -o hk-demo-jib.zip

Unzip the demo-jib.zip file and cd to the project root directory

unzip hk-demo-jib.zip -d hk-demo-jib && cd hk-demo-jib

Add Jib Maven plugin into your project

Modify the pom.xml file to add Jib Maven plugin

<build>  
  ...
  <plugins>
    ...
    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>jib-maven-plugin</artifactId>
      <version>2.5.2</version>
      <configuration>
        <to>
          <image>hk/jib</image>
        </to>
      </configuration>
    </plugin>
    ...
  </plugins>
  ...
</build>  

hk/jib is the container image name, you can set it to your own value

Build image to your local Docker

Use command mvn compile jib:dockerBuild if you would like to build an image to your local Docker

./mvnw compile jib:dockerBuild

The entry point is auto-configured, you can find details in the console output

Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.hellokoding.tutorials.DemoApplication]  

Type the below command to check the resulting image

docker images  

You would see a docker image name hk/jib and the tag is latest

Run a container from the created image

Use docker run command to create and start the Spring Boot container from the created image

docker run -p 8080:8080 hk/jib  

A web application is served at localhost:8080, you can cURL the endpoints in another terminal

curl localhost:8080/actuator  

Tag the build image

Use the option -D image=<MY IMAGE> to tag the image version

./mvnw compile jib:dockerBuild -D image=hk/jib:v1

You can also tag the image with build timestamp by adding the following property to your pom.xml

<properties>  
  <maven.build.timestamp.format>yyyyMMdd-HHmmssSSS</maven.build.timestamp.format>
</properties>  

Then in the jib-maven-plugin plugin configuration

<configuration>  
  <to>
    <image>hk/jib:${maven.build.timestamp}</image>
  </to>
</configuration>  

Run the build command again and check the tag value of the resulting images

./mvnw compile jib:dockerBuild && docker images

Conclusion

In this tutorial, we learned about using Jib to build an efficient container image without the need for preparing a Dockerfile