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>
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
4. π§ Build Docker Image Using Jib
Now, generate the Docker image:
mvn compile jib:dockerBuild
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
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
-
-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
β 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
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)