Docker

Google Jib

Hello there!! Recently Google introduced open sourced plugin called Jib. A plugin helps easily build and push Java Docker images to a repo. It does not require you to write a Dockerfile or have docker installed. Plugin available for Maven and Gradle.

As of now, Jib supports these container registries:
– Google Container Registry (GCR);
– Amazon Elastic Container Registry (ECR);
– Docker Hub Registry;

I created a simple Spring Boot app with Gradle:

In my example, I will push Java Docker images to Docker Hub. It can be configured for other repos also, look here.
Let’s see what we have to configure in gradle.build file to make this thing working:

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

// ***jib plugin***
plugins {
    id 'com.google.cloud.tools.jib' version '0.9.8'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

// in this section, we specify our configurations for a plugin
jib {
    to { //  'to' means where we are going to push our image
        image = '{docker hub username}/{image-name}' // set your docker hub username/id and give a name to your Docker image
        auth {
            username = '{docker hub username}'// set your docker hub username/id
            password = '{docker hub password}'// set your docker hub password
        }
    }
}

With those few lines of code added to your app, next step you need to do is simply run in the root of the project:

gradle jib

And voilà, your project is pushed to Docker Hub.

Jib by default takes as base image – gcr.io/distroless/java. You can overwrite this by specifying your own base image, like so:

...
jib {
  from {
    image = 'openjdk:alpine' //your base image
  }
  to {
    image = '{docker hub username}/{image-name}'
  }
}
...

There are other things you can configure in your jib task, pls refer to doc.

Author

Cahlen Humphreys