DEV Community

Cover image for Build optimized Docker images with JIB Maven Plugin
William
William

Posted on

Build optimized Docker images with JIB Maven Plugin

Jib is a plugin that builds optimized Docker images for Java applications. It organizes image layers efficiently, separating dependencies, resources, and application code, which reduces image size and speeds up rebuilds. with Maven, Jib is configured in the pom.xml file, enabling seamless integration into the build lifecycle.

How it works:

  1. Project analysis: Jib reads the pom.xml to identify dependencies, classes, and resources.
  2. Image building: Creates optimized layers (dependencies, resources, classes) and assembles the Docker image.
  3. Publishing (optional): Pushes the image to a configured registry, such as Docker Hub.
  4. Local execution (optional): Can run the image locally if Docker is available.

Jib doesn’t require a Dockerfile but allows customization through the pom.xml. It supports fast builds with reusable layers and is ideal for CI/CD pipelines.

Advantages of Jib with Maven

  • No Dockerfile: Simple configuration in pom.xml.
  • Fast builds: Reuses unchanged layers.
  • Portability: Doesn’t depend on a local Docker daemon.
  • Security: Supports authentication for private registries.
  • CI/CD integration: Compatible with tools like Jenkins and GitHub Actions.

Basic Configuration Example

Let’s set up Jib in a Maven project to create a Docker image for a simple Spring Boot application and publish it to Docker Hub.

Prerequisites:

  • Maven project with Java (Spring Boot).
  • Docker Hub account (for publishing).
  • Maven installed.

Step 1: Configure the pom.xml

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>3.4.3</version>
        <configuration>
          <from>
            <image>openjdk:17-jdk-slim</image>
          </from>
          <to>
<image>docker.io/youruser/${project.artifactId}:${project.version}</image>
          </to>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • from: Specifies the base image (here, openjdk:17-jdk-slim).
  • to: Defines the generated image name and registry (replace youruser with your Docker Hub username).

Step 2: Build the Image

Run the Maven command to build the image:

mvn compile jib:build
Enter fullscreen mode Exit fullscreen mode

This compiles the project and builds the Docker image, pushing it directly to Docker Hub. If authentication isn’t configured, log in to Docker Hub.

Step 3: Test Locally (Optional)

To build the image without pushing to a registry, use:

mvn compile jib:dockerBuild
Enter fullscreen mode Exit fullscreen mode

Then, run the image with:

docker run -p 8080:8080 youruser/my-app:latest
Enter fullscreen mode Exit fullscreen mode

Conclusion

Jib streamlines Docker image creation for Java projects, integrating seamlessly with Maven to build and publish optimized images without Dockerfiles. Its simplicity and efficiency make it ideal for both small projects and complex CI/CD pipelines. Start using Jib to enhance your development workflow! 🚀

Official documentation: https://github.com/GoogleContainerTools/jib

Top comments (0)