Prerequisites
Basic docker knowledge & commands, can go through this blog Docker-For-Beginners by InnovationM.
Introduction
In this blog I will guide you step-by-step how to use google jib plugin with docker.
What is Jib ?
Jib is part of Google Container Tools which builds optimized Docker and OCI images for your java application and push them to your public or private docker repositories without the need for Docker daemon.
Why Jib?
So in order to know what is jib and how to use it, you need to know why we need Jib in the first place.
As we already know docker containers are bringing developers closer than ever to
| Write once, run anywhere
What we get is the docker image which can be run anywhere which makes deployment easy.
But still containerizing a Java application is no simple task.
- First you have to create a file known as DockerFile
- Run that file using docker build command to build the image
- Wait for the build to complete
- Finally push the image to the remote public/private repository
So why should the developer be concerned with this, and this gets tedious in the case suppose you have a microservice application, so you will have to repeat the same process for all microservices you are working on.
To address this challenge, google made an open source java containerizer that lets Java developers build containers using the Java tools they know.
Jib is a fast and simple container image builder that handles all the steps of packaging your application into a container image. It does not require you to write a Dockerfile or have docker installed, and it is directly integrated into Maven and Gradle
Here is the visual representation of traditional docker flow vs jib flow :-
Docker build flow:
Jib build flow:
See, all those steps
- Building the JAR and creating the Dockerfile
- then using both via docker command to create the image in your local machine
- then finally pushing to your public/private registry like Docker Hub Registry, Google Container Registry , Amazon Elastic Container Registry
are all gone. Jib is implemented in Java and runs as part of your Maven or Gradle build. You do not need to maintain a Dockerfile, run a Docker daemon, or even worry about creating a fat JAR with all its dependencies. Since Jib tightly integrates with your Java build, it has access to all the necessary information to package your application.
How to use Jib ?
In this blog I will be showing you using Maven plugin.
1. Add this plugin in your <plugins>
attribute in pom.xml
<plugin>
<groupId>com.google.cloud. tools</groupId>
<artifactId>jib-maven-plugin</ artifactId>
<version>3.4.0</version>
<configuration>
<to>
<image>docker.io/{Username}/${ project.artifactId}:${project. version}</image>
<tags>
<tag>latest</tag>
</tags>
</to>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Note:
- Make sure your project artifact Id is in small letters, as per docker image naming conventions.
- Here I am using Docker Hub, so docker.io but you can change accordingly for GCR or ECR
- Be sure to put your docker hub registry username in place of {Username}
In above code :-
<image>
defines the location where your image will be created where image name & version is going to be your project artifactId & version
<executions>
defines that jib will run during your maven package phase. Whenever you runmvn package
command, jib will run.
2. But how it can push in your private/public repository unless it has the credentials to your registry account.
- There are multiple ways to provide credentials to jib. You can go through official jib documentation to configure this Jib Authentication Methods
- If you have docker installed then it can automatically take your credentials.
- But if it doesn’t work then Registry credentials can be added to your Maven settings. This is already explained in the link above, but still lets see
- Navigate to your .m2 repository and create a new file named âsettings.xmlâ
- Add this code to your file
<settings>
<servers>
<server>
<id>registry-1.docker.io</id>
<username>{Username}</ username>
<password>{Create token in your docker hub and put it here}</password>
</server>
</servers>
</settings>
- Now jib will use this to authenticate.
Note: using this method is not recommended, as it is hardcoding your credentials in a file. Google recommends using Docker Credential Helpers.
3. Now run mvn package
command, then after that execute mvn jib:build
command.
You should see process going on, jib usually takes some time. In a microservices application, depending upon your number of services, it can take minutes.
Use Jib in a Microservices application using Profiles
- Put the plugin in
<pluginManagement>
of your parent pom.xml
- Put this code in
<project>
attribute of all your services pom.xml
<profiles>
<profile>
<id>build-docker-image</id>
<build>
<plugins>
<plugin>
<groupId>com.google.cloud. tools</groupId>
<artifactId>jib-maven-plugin</ artifactId>
<version>3.4.0</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
- Now inside your parent directory execute
mvn package -P build-docker-image
command, which will package & build + push image to your registry for all services with the above profile code.
- This make easy as you don’t have to manually go, execute Dockerfile for services and push them.
Summary
Google’s Jib is a plugin that simplifies the process of containerizing Java applications for Docker. It eliminates the need for a Dockerfile, Docker daemon, or creating a fat JAR with all dependencies. Jib builds optimized Docker and OCI images and pushes them to public or private Docker repositories. It integrates directly into Maven and Gradle, and can be configured to run during the Maven package phase. Jib can automatically use Docker credentials, or they can be added to Maven settings. The plugin can also be used in a microservices application using profiles.