Zero-Dockerfile Spring Boot Containerization Using Jib

In this post, we’ll explore how Jib works, why it’s different from traditional Docker builds, and how to use it to containerize a Spring Boot application without a Dockerfile.

· Prerequisites
· Overview
∘ What is Jib?
∘ How Jib Works
∘ The Benefits of Jib
· Containerizing a Spring Boot App with Jib and Maven
∘ Add the Jib Plugin
∘ Build the Container Image
· Advanced Configuration
· Conclusion
· References


Prerequisites

This is the list of all the prerequisites:

  • Spring Boot 3
  • Maven 3.6.3 or later
  • Java 21
  • IntelliJ IDEA, Visual Studio Code, or another IDE

Overview

What is Jib?

Jib is an open-source tool developed by Google that builds optimised container images for Java applications. Its key features include:

  • No Dockerfile needed
  • No Docker daemon needed (unless you want to push to local Docker)
  • Layered image builds optimized for Java artifacts
  • Fast incremental builds
  • Reproducible builds (byte-for-byte identical)
  • Secure base images with automatic updates

It is available as plugins for Maven and Gradle and as a Java library.

How Jib Works

Whereas traditionally a Java application is built as a single image layer with the application JAR, Jib’s build strategy separates the Java application into multiple layers for more granular incremental builds. When you change your code, only your changes are rebuilt, not your entire application. These layers, by default, are layered on top of an OpenJDK base image, but you can also configure a custom base image.

The Benefits of Jib

  • No Dockerfile Needed: Jib handles the entire container construction process for you.
  • Fast: Jib offers fast, incremental builds by separating your app into layers (dependencies, resources, classes) and only rebuilding the layers that change.
  • Reproducible: Because Jib uses declarative build metadata, it can produce reproducible container images as long as the inputs stay the same.
  • Daemonless: You don’t need a Docker daemon running to build an image. This simplifies CI/CD environments and improves security.
  • Security by Default: Jib builds on the distroless Java image by default, a minimal OS image that contains only your application and its runtime dependencies. No shell, no package manager, and a drastically reduced attack surface.

Containerising a Spring Boot App with Jib and Maven

Let’s start by creating a simple Spring Boot project from start.spring.io, with the Spring Web dependency.

We’ll use Maven for this post; Gradle steps are similar.

Let’s expose a REST API endpoint

@RestController
@RequestMapping("/api")
@SpringBootApplication
public class SpringBootJibApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootJibApplication.class, args);
}

@GetMapping
public String index() {
return "Greetings: Spring Boot Containerization Using Jib!";
}

}

When accessed, it returns a plain text message with the current date and time:

Add the Jib Plugin

All we need to do is add the Jib Maven plugin to the pom.xml. No additional configuration is required to get started!

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<to>
<image>spring-boot-jib</image> <!-- Required - Configures the target image to build your application to.-->
</to>
</configuration>
</plugin>

Check for the latest version on: https://mvnrepository.com/artifact/com.google.cloud.tools/jib-maven-plugin

Build the Container Image

Build and push an image

To build your image and push it directly to a remote registry (e.g., Docker Hub or Google Container Registry), make sure you’re authenticated with that registry beforehand.

mvn compile jib:build

Build to local Docker

Jib can also build your image directly to a Docker daemon. This uses the docker command line tool and requires that you have docker available on your PATH.

mvn compile jib:dockerBuild

This requires Docker to be running, but not a Dockerfile.

After runningjib:dockerBuild, we can run the new container just like any other Docker image:

docker run -p 8080:8080 spring-boot-jib:latest

Then, try to call the endpoint

It works fine.

Advanced Configuration

Although Jib works out of the box, it’s also highly configurable. Below are some common customizations you can include within the <configuration> section of your pom.xml:

Base image customization

<from>
<image>eclipse-temurin:17-jre</image> <!-- Use the official Eclipse Temurin JRE -->
</from>

Setting JVM Flags

<container>
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
<jvmFlag>-Xdebug</jvmFlag>
</jvmFlags>
</container>

Set container entrypoint/user/environment

<configuration>
<container>
<ports>
<port>8080</port>
</ports>
<environment>
<SPRING_PROFILES_ACTIVE>prod</SPRING_PROFILES_ACTIVE>
</environment>
<user>1000</user>
</container>
</configuration>

You can find all available configuration options in the documentation here.

Conclusion

🏁 Well done !!. In this post, we explored how Jib enables Spring Boot developers to build container images without needing to touch a Dockerfile.

Jib offers an elegant approach to containerizing Java applications by embedding the process within your build workflow. The “zero-Dockerfile” model delivers tangible benefits, including improved speed, enhanced security, and greater build reproducibility.

The complete source code is available on GitHub.

Support me through GitHub Sponsors.

Thank you for reading!! See you in the next post.

References

👉Link to Medium blog

Related Posts