How to Streamline the Java Application Containerization Process

With an estimated 70% of Java applications running inside a container, Docker has become the norm for the Java community. Stack Overflow recently found that Docker ranked as both the most-desired and the most-used developer tool.

Given the significance of containerization in platform engineering, companies are always on the lookout to automate and improve their application’s lifecycle to ensure quality and efficiency. One of the challenges of building Docker images is to write quality Dockerfiles; criteria include container launch speed and size, structure and standard of the Dockerfile, security, and specific flags to optimize the JVM configuration. Application developers are generally the ones creating and maintaining these Dockerfiles and may not always have proper training.

Fortunately, the open source community has come up with some useful solutions that I will explore in this article.

Jib

Jib, an open source project by Google, streamlines containerization by allowing developers to build Java container images without requiring Docker expertise or editing a Dockerfile. The tool is available via Maven and Gradle plugins, as a library and a command-line interface. It optimizes the build process by utilizing layering. It only rebuilds and pushes the layers of the application that have changed, resulting in faster and more efficient image builds. It also supports multi-module projects, allowing developers to easily containerize complex Java applications. It automatically handles dependencies between modules and ensures that each module’s image is built efficiently.

Once your image is ready, the tool integrates with container registries, enabling secure image builds and pushes. And since Jib doesn’t rely on a local Docker daemon, it can be used in various environments, including local development machines, CI/CD pipelines and cloud-based build services. This flexibility makes it easy to incorporate Jib into existing workflows.

Once set up, using Jib on Maven is as simple as this:

# Builds to a container image registry.

$ mvn compile jib:build

# Builds to a Docker daemon.

$ mvn compile jib:dockerBuild

That’s it. The project is well maintained and, with 13,000 stars on GitHub, is definitely a community favorite!

Buildpacks

If you like the idea of Jib, you will certainly love Buildpacks. The concept – which is to go from application code to a running image – was initially created by Pivotal and Heroku and eventually became Cloud Native Buildpacks managed by the CNCF.

Similar to Jib, it is very easy to use with a single command and no Dockerfile knowledge is required to package your Java application. Developers can use the pack CLI, Maven plugin or Gradle plugin.

Buildpacks offer the same features as Jib with several additional ones, such as the ability to generate an SBOM, an advanced caching mechanism, and the ability to easily update base images without having to rebuild those. Buildpacks are compatible with most modern languages such as Node.js, .NET Core, Go, Python, PHP and Ruby.

To build a Buildpack, you need a base image that you can either build yourself or use one that is crafted by many providers such as Google, Heroku and Paketo. The advantage of using these images is that you can benefit from the knowledge of these companies and communities who build these with best practices for every stack. Additionally, they also have a quick turnaround time when it comes to updating any OS or library security fix.

Similar to Jib, once installed, you can build a container image of a Java application with one command:

pack build samples/java --path java/maven

Conclusion

Using Jib or Buildpacks to build your Java container images is a must-do. Not only does it make the job much faster and ensure that your images are correctly built, but it also automates the process — meaning that you can integrate it the DevOps way as part of a continuous delivery pipeline. Both tools have advantages; while Jib is faster at building images that are also smaller, Buildpacks offers more features and supports most modern programming languages. Whatever tool you pick, you cannot go wrong!

Sylvain Kalache

Sylvain is an entrepreneur who has worked in the tech industry for over a decade. He provides PR services, such as ghostwriting, media pitching, and communication strategy for tech startups. He is the co-founder of a software engineering school, its students get hired by leading companies like Google, NASA, Tesla, and Apple. Sylvain started his career as Site Reliability Engineer, working for companies like SlideShare and LinkedIn.

Sylvain Kalache has 4 posts and counting. See all posts by Sylvain Kalache