Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Generating Docker Images using Google Jib

Technology:

generating-docker

Containers are used deploy applications in an isolated environment, once we generate the container images can be deployed number of machines, but for a Java application building an image is not an easy way, we need to write a docker file, run a docker daemon as root, wait for build completion and finally need to push the image to repository. Google developed jib, an open source library, using this building and pushing image to registry in an easy way.

Jib provides maven and Gradle plugin using which we can generate the container images easily, as it takes cares all packing steps like generating Dockerfile, creating docker daemon to create a container image and push to Docker Registry based on configuration. If the project need other dependencies which are not part project class-path, then we can specify the files/folder to Jib maven plugin to include it in container image.

The typical Docker Build System:

generating-docker

Jib Build System:

generating-docker

Advantages of using Google Jib library:

  1. Simple: It is written in Java and it run as a part of Maven and Gradle plugin. For traditional way we need to run docker daemon as root user, we need to provide Dockerfile and create flat jar (application jar and its dependencies). Jib is integrating with the application it has the all the required information need to build image.Jib is one of the Maven/Gradle plugin, we can automate the Container Image generation using Jenkins (Continuous Integration Tool).
  2. Fast: like Docker it takes the advantage of image layering and registry caching to achieve fast, incremental builds. It reads all application configuration and organizes the application into different number of layers and rebuilds the layer if any changes are there. When we execute the Jib tasks it will watch for changes after last build, it only builds necessary changes, and build layer which are related to changes, instead of pushing all layers it will only the push modified layers.
  3. Reproducible – Jib plugin is built from Maven/ Gradle build files (pom.xml and build.gradle) files to create container images, if inputs are the same to jib it will create reproducible container image.

Integrating Jib to our projects:

Current we are going to learn for Maven build system, similar steps involved for Gradle system also.

Adding Jib plugin in pom.xml file:

<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>0.9.9</version> <configuration> <from> <image>openjdk:8u171-alpine</image> <auth> <username> <!—docker-hub-username-- > </username> <password> <!—docker-hub-password-- > </password> </auth> </from> <to> <image> <!—docker-hud-userid--->/${project.artifactId}:${project.version}</image> <auth> <username> <!—docker-hub-username-- > </username> <password>> <!—docker-hub-password-- > </password> </auth> </to> <container> <jvmFlags> <jvmFlag>-Xms512m</jvmFlag> <jvmFlag>-Xmx512m</jvmFlag> </jvmFlags> </container> </configuration> </plugin>

See jib-maven-plugin’s Extended Usage tables for more options.

By default, Jib plugin will connect to Docker public Registry for pulling and pushing the images. we need to provide the credential of it in auth section.

Present blog we will learn how to generate and push the spring boot 2 application using jib plugin.

We can create the spring boot 2 application by navigating https://start.spring.io or we can also maven architypes to generate the application.

-c url "https://start.spring.io/starter.tgz" -d name="springboot2-docker-demo" -d description="Simplifying Docker images for Spring Boot 2 apps" -d bootVersion=2.0.3.RELEASE -d dependencies=actuator,web -d language=java -d type=maven-project -d baseDir=springboot2-docker-demo -d groupId=com.asimio.demo -d artifactId=springboot2-docker-demo -d version=0-SNAPSHOT | tar -xzvf –

Add the job maven plugin as specified above.

We can specify the docker hub credentials in pom.xml file or we can use docker credentials helper as given in the below link.

https://github.com/docker/docker-credential-helpers#available-programs

Building Spring Boot Artifact:

We can use mvn clean package to generate the output files for java application and Java software development company.

generating-docker

The above step is independent of Jib library.

Creating Docker Context:

We can use mvn jib:exportDockerContext command to generate the docker context.

generating-docker

If we navigate output directory we can observe jib-docker-context folder is created and it will contain all required files and Dockerfile used to build the Docker image.

generating-docker

Note: The content of target/jib-docker-context could be used to build a Docker image using docker build -t sravan4rmhyd/springboot2-docker-demo:1.0.1 target/jib-docker-context command.

Docker file looks likes as below:

generating-docker

Jib plugin is also used create docker daemon to push the images to Docker registry.

Setting up the Docker Daemon:

We can add the below server in .m2/settings,xml file

<server> <id>registry.hub.docker.com</id> <username>xxxx</username> <password>xxxx</password> </server>

Where username and password are the docker hub credentials, and its recommend using encrypted password as specified in the below link.

https://maven.apache.org/guides/mini/guide-encryption.html

once the it is done, we can invoke mvn jib:build command to push the image to repository.

generating-docker

We can see the uploaded image in docker hub registry.

generating-docker

We can download the source code of the application in below url:

https://github.com/sravan4rmhyd/spring-boot2-jib.git

Conclusion:

Google jib is the open source library which is used to build docker images and push to registry without need of Docker daemon, it reduces all complexities in writing Dockerfile and build procedures.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients