DEV Community

DevCorner2
DevCorner2

Posted on

πŸš€ Containerizing Java Applications with Google Jib (No Dockerfile Needed)

Traditionally, containerizing a Java application requires writing a Dockerfile, building an image with Docker, and then pushing it to a registry. While effective, this approach introduces extra steps and dependencies.

Google Jib eliminates that complexity. It integrates directly into your build tool (Maven or Gradle), allowing you to build and push container images straight from your Java project β€” no Dockerfile, no docker build, and not even Docker itself on your machine.


πŸ› οΈ Why Use Jib?

  • No Docker daemon required (works even if Docker isn’t installed).
  • Build directly from Maven/Gradle (seamless developer experience).
  • Layered image caching for faster incremental builds.
  • Optimized images with minimal overhead.

πŸ“Œ Step 1: Add Jib to Your Build Tool

For Maven:

Add the plugin to your pom.xml:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>jib-maven-plugin</artifactId>
  <version>3.4.2</version>
</plugin>
Enter fullscreen mode Exit fullscreen mode

For Gradle:

Add the plugin in build.gradle:

plugins {
  id 'com.google.cloud.tools.jib' version '3.4.2'
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 2: Configure Image Destination

Tell Jib where to push your image.

Example for a private registry (say GCP Artifact Registry or Harbor):

Maven

<configuration>
  <to>
    <image>myregistry.example.com:5000/myteam/myapp:1.0</image>
  </to>
</configuration>
Enter fullscreen mode Exit fullscreen mode

Gradle

jib {
  to {
    image = 'myregistry.example.com:5000/myteam/myapp:1.0'
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 3: Build and Push the Image

  • With Maven:
mvn compile jib:build
Enter fullscreen mode Exit fullscreen mode
  • With Gradle:
./gradlew jib
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ This command builds your app, creates a Docker/OCI image, and pushes it directly to the registry.


πŸ“Œ Step 4: Build Image Locally (Optional)

If you want the image only on your local machine (not pushed to a registry):

  • Maven:
mvn compile jib:dockerBuild
Enter fullscreen mode Exit fullscreen mode
  • Gradle:
./gradlew jibDockerBuild
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 5: Verify the Image

Pull from your private registry (if you pushed):

docker pull myregistry.example.com:5000/myteam/myapp:1.0
Enter fullscreen mode Exit fullscreen mode

Or run locally if built with jibDockerBuild:

docker run -p 8080:8080 myregistry.example.com:5000/myteam/myapp:1.0
Enter fullscreen mode Exit fullscreen mode

⚑ Pro Tips

  • Environment-specific configs: You can set image name, tags, and credentials via application.properties or CI/CD pipelines.
  • No JAR needed: Jib packages classes/resources directly, skipping the need for mvn package before building.
  • Faster CI/CD: Great for Kubernetes deployments with GitOps workflows.
  • Secure builds: Works without requiring root access to Docker.

βœ… Conclusion

With Google Jib, you can skip writing Dockerfiles and bypass Docker altogether. It integrates smoothly with Maven/Gradle, builds optimized images, and pushes them directly to your private or public registry.

This approach is especially valuable in enterprises where:

  • Docker isn’t installed on build servers.
  • Teams want faster, repeatable, and secure builds.
  • CI/CD pipelines need minimal dependencies.

Top comments (0)