TODO: Add badges.
Jib is a tool for building container images for your Java applications.
- Fast - Your Java application gets broken down into multiple layers, separating dependencies from classes. Deploy your changes faster - don’t wait for Docker to rebuild your entire Java application.
- Native - Reduce your CLI dependencies. Build your Docker image from within Maven and push to any registry of your choice. No more writing Dockerfiles and calling docker build/push.
In your Maven Java project, add the plugin to your pom.xml
:
<plugin>
<groupId>com.google.com.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>0.1.0</version>
<configuration>
<registry>myregistry</registry>
<repository>myapp</repository>
</configuration>
</plugin>
Configure the plugin by changing registry
, repository
, and credentialHelperName
accordingly.
Make sure you have the docker-credential-gcr
command line tool.
For example, to build the image gcr.io/my-gcp-project/my-app
, the configuration would be:
<configuration>
<registry>gcr.io</registry>
<repository>my-gcp-project/my-app</repository>
<credentialHelperName>gcr</credentialHelperName>
</configuration>
Make sure you have the docker-credential-ecr-login
command line tool.
For example, to build the image aws_account_id.dkr.ecr.region.amazonaws.com/my-app
, the configuration would be:
<configuration>
<registry>aws_account_id.dkr.ecr.region.amazonaws.com</registry>
<repository>my-app</repository>
<credentialHelperName>ecr-login</credentialHelperName>
</configuration>
Build your container image with:
mvn compile jib:build
Subsequent builds would usually be much faster than the initial build.
Having trouble? Let us know by submitting an issue.
You can also bind jib:build
to a Maven lifecycle such as package
by adding the following execution to your jib-maven-plugin
definition:
<plugin>
<groupId>com.google.com.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
...
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Then, you can build your container image with just:
mvn package
Extended configuration options provide additional options for customizing the image build.
Field | Default | Description |
---|---|---|
from |
gcr.io/distroless/java |
The base image to build your application on top of. |
registry |
Required | The registry server to push the built image to. |
repository |
Required | The image name/repository of the built image. |
tag |
latest |
The image tag of the built image (the part after the colon). |
jvmFlags |
None | Additional flags to pass into the JVM when running your application. |
credentialHelperName |
Required | The credential helper suffix (following docker-credential- ) |
mainClass |
Uses mainClass from maven-jar-plugin |
The main class to launch the application from. |
In this configuration, the image is:
- Built from a base of
openjdk:alpine
(pulled from Docker Hub) - Pushed to
localhost:5000/my-image:built-with-jib
- Runs by calling
java -Xms512m -Xdebug -Xmy:flag=jib-rules -cp app/libs/*:app/resources:app/classes mypackage.MyApp
<configuration>
<from>openjdk:alpine</from>
<registry>localhost:5000</registry>
<repository>my-image</repository>
<tag>built-with-jib</tag>
<jvmFlags>
<jvmFlag>-Xms512m</jvmFlag>
<jvmFlag>-Xdebug</jvmFlag>
<jvmFlag>-Xmy:flag=jib-rules</jvmFlag>
</jvmFlags>
<mainClass>mypackage.MyApp</mainClass>
</configuration>
Whereas traditionally a Java application is built as a single image layer with the application JAR, Jib's build strategy breaks 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 a distroless base image.
See also rules_docker for a similar existing container image build tool for the Bazel build system.
These limitations will be fixed in the future.
- Does not build OCI images.
- Pushing to Docker Hub does not seem to work.
- Cannot build directly to a Docker daemon.
- Cannot use a private image as a base image.
If a question you have is not answered before, please submit an issue.
See rules_docker for a similar existing container image build tool for the Bazel build system. The tool can build images for languages such as Python, NodeJS, Java, Scala, Groovy, C, Go, Rust, and D.
Other authentication methods will be added in our next release (v0.2.0
).
The plugin attaches a default entrypoint that will run your application automatically.
When running the image, you can override this default entrypoint with your own custom command.
See docker run --entrypoint
reference for running the image with Docker.
See Define a Command and Arguments for a Container for running the image in a Kubernetes Pod.
Jib packages your Java application into the following paths on the image:
/app/libs/
contains all the dependency artifacts/app/resources/
contains all the resource files/app/classes/
contains all the classes files
Running commands like apt-get
slows down the container build process. We do not recommend or support running commands as part of the build.
However, if you need to run commands, you can build a custom base image. You can then use this custom base image in the jib-maven-plugin
by adding the following configuration:
<configuration>
<from>custom-base-image</from>
</configuration>
We currently do not support adding a custom directory to the image. If your application needs to use custom files, place them into your application's resources directory (src/main/resources
by default). These resource files will be available on the classpath.
We currently do not support building to a local Docker daemon. However, this feature is in the pipeline and will be added in the future.
You can still docker pull
the image built with jib-maven-plugin
to have it available in your local Docker daemon.
TODO: Provide solution.
TODO: Provide solution.
To tag the image with a simple timestamp, add the following to your pom.xml
:
<properties>
<maven.build.timestamp.format>yyyyMMdd-HHmmssSSS</maven.build.timestamp.format>
</properties>
Then in the jib-maven-plugin
configuration, set the tag
to:
<configuration>
<tag>${maven.build.timestamp}</tag>
</configuration>
You can then use the same timestamp to reference the image in other plugins.