DEV Community

SOVANNARO
SOVANNARO

Posted on

πŸ³πŸ’‘ What Is Jib?

Jib is a tool created by Google to help Java developers create Docker images for their applications without writing a Dockerfile and even without installing Docker locally (optional). It works only for Java applications.

Think of Jib as a shortcut that turns your Java app into a Docker image easily.


πŸ›  Why Use Jib Instead of Other Tools?

  • Only for Java apps (not Node.js, Python, etc.).
  • No need to write a Dockerfile.
  • Faster than Buildpacks (another tool for creating Docker images).
  • Can work without Docker installed, by pushing images directly to a remote repository (like Docker Hub).

πŸš€ Steps to Use Jib with Maven

Let’s say you want to containerize a Java microservice named cards using Maven.

1. πŸ“¦ Go to Jib GitHub

Link: https://github.com/GoogleContainerTools/jib

There you’ll find guides on how to use Jib with Maven or Gradle.

Since you're using Maven, follow the Maven instructions.


2. 🧩 Add Jib Plugin to pom.xml

Open your cards service’s pom.xml file and add the following plugin:

<build>
  <plugins>
    <!-- existing plugins like spring-boot-maven-plugin -->

    <plugin>
      <groupId>com.google.cloud.tools</groupId>
      <artifactId>jib-maven-plugin</artifactId>
      <version>3.3.2</version>
      <configuration>
        <to>
          <image>your-dockerhub-username/cards:S4</image>
        </to>
      </configuration>
    </plugin>
  </plugins>
</build>
Enter fullscreen mode Exit fullscreen mode

Make sure to:

  • Replace your-dockerhub-username with your actual Docker Hub username.
  • Ensure packaging is set to jar.

3. 🧹 Clean Project

Before building, clean the previous builds:

mvn clean
Enter fullscreen mode Exit fullscreen mode

4. πŸ”§ Build Docker Image Using Jib

Now, generate the Docker image:

mvn compile jib:dockerBuild
Enter fullscreen mode Exit fullscreen mode

This will create a Docker image using your local Docker (no need for a Dockerfile).

βœ… If successful, it will build the image very quickly (in the demo: ~11 seconds).


5. πŸ‹ Check Docker Image

Run:

docker images
Enter fullscreen mode Exit fullscreen mode

You’ll see the cards image listed there.


6. ▢️ Run the Docker Container

Now run your container:

docker run -d -p 9000:9000 your-dockerhub-username/cards:S4
Enter fullscreen mode Exit fullscreen mode
  • -d means run in background.
  • -p 9000:9000 means map local port 9000 to container port 9000.

7. πŸ“¬ Test the API

Use Postman or browser to test if the service is running:

http://localhost:9000/api/create
Enter fullscreen mode Exit fullscreen mode

βœ… If you get a response, everything is working fine!


❓Why the "43 Years Ago" Timestamp?

You might see that the Docker image says it was created "43 years ago".

  • This is NOT a bug!
  • It’s a feature to optimize caching. If nothing changes, the image content and date stay the same, which makes builds faster and more efficient.

πŸ’» What If You Don’t Have Docker Installed?

You can still build the Docker image and push it to a remote registry (like Docker Hub) directly.

Run this command instead:

mvn compile jib:build
Enter fullscreen mode Exit fullscreen mode

This will push the image to Docker Hub (or GCR/ECR) instead of building it locally. Just make sure your image name is correct and you are logged in.


βœ… Summary

Step What You Do
1️⃣ Add Jib plugin to pom.xml
2️⃣ Clean your project
3️⃣ Run mvn compile jib:dockerBuild
4️⃣ Check the image with docker images
5️⃣ Run it with docker run
6️⃣ Test it using Postman
🧠 Remember: No Dockerfile needed!

πŸ†š Jib vs Buildpacks

Feature Jib Buildpacks
Language Support Only Java Java, Python, Node, etc.
Dockerfile Required ❌ No ❌ No
Fast Build βœ… Yes ❌ Slower
No Docker Needed βœ… Yes (optional) ❌ No

Top comments (0)