What is Jib?

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.

Containerize your Gradle Java project

Setup

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

configuration

Using Google Container Registry (GCR)

1
jib.to.image = 'gcr.io/my-gcp-project/my-app'

Using Docker Hub Registry

1
jib.to.image = 'my-docker-id/my-app'

Build Your Image

Build your container image with:

1
gradle jib

Build to Docker daemon

1
gradle jibDockerBuild

Run jib with each build

You can also have jib run with each build by attaching it to the build task:

1
tasks.build.dependsOn tasks.jib

Multi Module Projects

The example project consists of two microservices and a library:

  1. name-service - responds with a name

  2. shared-library - a project dependency used by name-service

  3. hello-service - calls name-service and responds with a greeting

Extended Usage

to : Configures the target image to build your application to.

from : Configures the base image to build your application on top of.

container : Configures the container that is run from your built image.

System Properties

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

Example

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:

Adding Arbitrary Files to the Image

Reference