16. July 2021
1 min

JIB – Build Docker images for your Java Application without a Docker Daemon

Do you use Docker on your system to build images for your Java application? On Windows I had problems configuring and/or starting up the daemon on VirtualBox. Yes, there is a WSL2 Docker installation. But if you are not on a current Windows 10 installation, then you cannot use it. You must configure Docker with virtualization software like Hyper-V or Virtual Box, etc. Or do you need a docker agent for building a container on the CI/CD pipeline? But the agent is “always” blocked by other projects

There is a tool by Google, called JIB (https://github.com/GoogleContainerTools/jib). JIB builds your Java application, containerizes it to a deployable image and pushes the image to a registry. It is fully configurable over Maven or Gradle.

There is one downside: It is not possible to run docker operations like “RUN”, “USER”, etc. Therefore, you need to setup up a base image and use this in your JIB configuration.

If you want to use JIB with Maven, you must configure the jib-maven-plugin:

jib-maven-plugin-configuration

Quelle: Ebehard Mayer

Then start the containerization with the command: mvn compile jib:build and you get the Output:

maven-build-output-of-jib

Quelle: Eberhard Mayer

The Maven plugin of JIB will first recompile the classes if needed. In the second build step, it generates the image and pushes it to the specified docker registry. In this example I have pushed the image to my Docker Hub repository: https://hub.docker.com/repository/docker/eberhardmayer/jib-example . Then you can use it from your registry in Kubernetes or other tools. I have set up the whole example at GitHub: https://github.com/eberhardmayer/jib-example

JIB gives you the possibility to create a Docker container via Gradle, Maven or a CLI without having a docker agent on your system. It also integrates well with tools like Maven and makes it easier to manage a CI/CD pipeline with it. Some scenarios will not fit with JIB, like running the key tool of Java on image creation. But it can help to simplify reproducible builds for your Java application.

Comment article