Jib builds optimized Docker and OCI images for your Java applications without a Docker daemon - and without deep mastery of Docker best-practices.
The Open Container Initiative is an open governance structure for the express purpose of creating open industry standards around container formats and runtimes. New tools for building container images aimed to improve Docker’s speed or ease of use. To make sure that all container runtimes could run images produced by any build tool, the community started the Open Container Initiative — or OCI — to define industry standards around container image formats and runtimes. Given an OCI image, any container runtime that implements the OCI Runtime Specification can unbundle the image and run its contents in an isolated environment.
In your Gradle Java project, add the plugin to your build.gradle:
1
2
3
plugins {
id 'com.google.cloud.tools.jib' version '3.3.1'
}
You can containerize your application easily with one command:
1
gradle jib --image=<MY IMAGE>
To build to a Docker daemon, use:
1
gradle jibDockerBuild
Make sure you have the docker-credential-gcr command line tool.
To build the image gcr.io/my-gcp-project/my-app, the configuration would be:
1
jib.to.image = 'gcr.io/my-gcp-project/my-app'
Make sure you have a docker-credential-helper set up
For example, to build the image my-docker-id/my-app, the configuration would be:
1
jib.to.image = 'my-docker-id/my-app'
Build your container image with:
1
gradle jib
1
gradle jibDockerBuild
jib with each buildYou can also have jib run with each build by attaching it to the build task:
1
tasks.build.dependsOn tasks.jib
The example project consists of two microservices and a library:
name-service - responds with a name
shared-library - a project dependency used by name-service
hello-service - calls name-service and responds with a greeting
build.gradle that sets some common configuration up for all projects,build.gradle with some custom configuration.settings.gradle defines which modules to include in the overall build.to : Configures the target image to build your application to.
--image command line option. If the tag is not present here :latest is implied.from : Configures the base image to build your application on top of.
openjdk:11-jre, docker://busyboxcontainer : Configures the container that is run from your built image.
Each of these parameters is configurable via commandline using system properties.
Jib’s system properties follow the same naming convention as the configuration parameters, with each level separated by dots
1
2
3
4
5
6
7
8
9
gradle jib \
-Djib.to.image=myregistry/myimage:latest \
-Djib.to.auth.username=$USERNAME \
-Djib.to.auth.password=$PASSWORD
gradle jibDockerBuild \
-Djib.dockerClient.executable=/path/to/docker \
-Djib.container.environment=key1="value1",key2="value2" \
-Djib.container.args=arg1,arg2,arg3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
jib {
from {
image = "openjdk:alpine"
}
to {
image = "localhost:5000/my-image/built-with-jib"
<!-- credHelper = 'osxkeychain' -->
tags = setOf("tag2", "latest")
}
container {
jvmFlags = listOf("-Dmy.property=example.value", "-Xms512m", "-Xdebug")
mainClass = "mypackage.MyApp"
args = listOf("some", "args")
ports = listOf("1000", "2000-2003/udp")
<!-- labels = [key1:'value1', key2:'value2']
format = 'OCI' -->
}
}
In this configuration, the image:
openjdk:alpine (pulled from Docker Hub)java -Dmy.property=example.value -Xms512m -Xdebug -cp app/libs/*:app/resources:app/classes mypackage.MyApp some argssrc/main/jib directory.jib folder to the target directory (/ by default) in the image, maintaining the same structure
Jib does not follow symbolic links in the container image. If a symbolic link is present, it will be removed prior to placing the files and directories.