First published July 18, 2018 in ITNEXT

Jib: Getting Expert Docker Results Without Any Knowledge of Docker

All of the containerization benefits, none of the complexity.

Google Jib logo

Introduction

Well have I got some news for you. If you’ve been following my latest blog posts, I’ve been talking a good deal about Docker, the amazing containerization platform making it easier than ever to develop and deploy web applications. The first post covered the Docker basics and the Docker CLI. The second , introduced Docker Compose, Docker’s more sophisticated, easier (in my opinion) to use, multi-service option.

Docker is a very powerful tool when used correctly, but it takes a minute to get your head around:

  • The commands,
  • The file structure,
  • The whole containerization concept, itself.

It’s not easy if you’ve not used it before, there’s a definite learning curve. But Google, ever the leader in technology, has just released a game changer…

Meet Jib

Jib, is a brand new, just released, Google Container Tools-based, Java plugin that will containerize your application.

Put simply:

Jib builds Docker and OCI images for your Java applications and is available as plugins for Maven and Gradle. — Jib, Github

What this means for us is:

  • No Dockerfile setup,
  • No customized scripting for Maven or Gradle,
  • Out of the box Docker image generation and publication.

Basically, no Docker complexity, but all of the Docker benefits.

How? A plugin, a very simple plugin in your project. I’ll show you just how simple below.

Implementing Jib in Gradle

There are both Maven and Gradle plugins for Jib. I’ll be using Gradle because that’s what I’m most familiar with.

We begin with the build.gradle file. First and foremost, make sure you’re working with Gradle 4.6 or later — this is required for Jib.

Next, the changes to the file. Inside of the build.gradle, in your plugins section, add this line.

plugins {
  id 'com.google.cloud.tools.jib' version '0.9.6'
}

Yes, it really is that easy to generate a Docker image now.

And that’s it. You’re done with the changes needed in your Java application.

Now, from the command line run the Gradle task ./gradlew jibDockerBuild. This will generate your local Docker image and give you its image name and version.

Ok, final step, the Docker run command in the terminal: docker run -p <host port: Docker app port> <image name: version>. And with this your application should start up.

I made a sample Java application and stored it in Github here, so you can see just how simple this really is.

I also left the Dockerfile in there, so you can see what used to be necessary to run Docker. Keep in mind, this project is only set up for building and running local images, if you want to push to Docker hub, see the Jib official documentation (and check back, I may do a follow up post on it).

For my project, you’d run the same ./gradlew jibDockerBuild, then this Docker run command: docker run -p 8080:8080 java-example:1.0.0-SNAPSHOT.

Sample Dockerized Java app started courtesy of Jib

This is what you’ll see when you go to http://localhost:8080 in your browser.

Before Jib, I needed an entirely separate Dockerfile instructing how to build my application to make magic like this happen.

Dockerfile

FROM openjdk:8-jdk-alpine

# create the directory for where Tomcat creates its working directories
VOLUME /tmp

# copy the project JAR file to the container renamed as 'app.jar'
COPY build/libs /app

# execute that JAR in the entry point below
# java -Djava.security.egd=file:/dev/./urandom -jar /app/java-example.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/java-example.jar"]

Look at this. And this is a simple, simple Java application. These Dockerfiles can (and usually are) much bigger and more complex.

Conclusion

Thanks to Jib, Dockerfiles can be a thing of the past — at least, for your Java applications. Another bonus is that if you have a docker-compose.yml file in your project, that can still be used in tandem with Jib.

Personally, I can’t wait to start dropping this one-liner into all of my team’s Spring Boot applications. With it, we can get the power and flexibility of a Docker container with none of the setup and necessary knowledge about Docker.

Even if you’re not a Docker expert, with Jib you can still reap the benefits, and look like a rockstar in the process. Enjoy!

Further References & Resources

Want to be notified first when I publish new content? Subscribe to my newsletter.