Do you need a simple way to build docker images for your java with maven applications? Use the jib maven plugin and let maven do the heavy lifting!

For a complete overview of all capabilities of the jib maven plugin take a look at the docs here. Or use another useful tutorial such a this one from Baeldung.

We will replace the Dockerfile with the jib-maven-plugin for an existing project from a previous post on GraphQL and Micronaut. The completed code for this post can be found on github here.

No Dockerfile

The goal in this section is to replace the existing Dockerfile with our plugin.

FROM openjdk:14-alpine
COPY target/graphql-cat-api-*.jar graphql-cat-api.jar
CMD ["java", "", "-Xmx128m", "-jar", "graphql-cat-api.jar"]

As you can see we define the base image, copy the executable jar, expose a port and state the command to run. But some of these steps are the same for all java applications and are omitted by jib as we can see now.


Here we reference the jib plugin we need and only specify the base image and the port. The resulting image name we would have needed in the docker build command is also present so the image can be tagged.

Of course there are many more configuration options for all the things you previously did in the Dockerfile. See here a complete list of the options.

Notice that we don’t have to specify the executable jar or the run command. This is abstacted away by the plugin.

Building with docker

Previously we built with docker like this.

docker build -t sybrenbolandit/graphql-cat-api:1.0.0 .

We have to specify the tag in the command. With the jib plugin the tag is already in the configuration. Building the image is done with the following command.

mvn compile jib:dockerBuild

This uses your local docker instance to build the image from the configuration. Let’s look at the output.

build output of jib maven plugin 1024x364 - JIB MAVEN PLUGIN

Notice that we see here how the entrypoint is automatically filled.

Building without docker

You can even build your image without running docker locally. This is done with the following command.

mvn compile jib:build

Running this command will probably fail! This is because jib is trying to push the image after building it and the credentials are not set.

You could set these credentials specific with the image.

  <username>## dockerhub username ##</username>
  <password>## dockerhub password ##</password>

Or you can define it generally in your Maven settings.xml. Here you define a server with the right credentials.

  <username>## dockerhub username ##</username>
  <password>## dockerhub password ##</password>

Now executing your maven build will build the docker image without using your docker deamon and push the image to dockerhub.

dockerhub tile with newly pushed image 1024x284 - JIB MAVEN PLUGIN

Hopefully you are now able to build your docker images with the jib maven plugin. Happy jibbing!