Dockerize Maven Jdk8 & Jdk11 LTS

Today we will talk about a way to build a docker image shipping the following tools :

  1. Maven (latest version)
  2. OpenJdk 8
  3. Openjdk 11 lts

In a next article, we will see how to use this image in order to build a multi modules Java project.

Finally we will go through the Continuous Integration process for our project using Github.

Why a new docker image for Java & Maven ?

Usually in order to compile a project using Maven and Java in a docker environment, you head up to the Maven official docker hub page and pick up the right version

However in case you need to use 2 different JDKs, you will notice that you won’t find what you need on the Maven docker hub page.

After some research on different resources, you will also understand that it is not that easy to find some maintained version that fits you.

More about Maven docker official images

If you browse the various tags provided on docker hub, you can take a closer look at a dockerfile (ex: AdoptOpenJdk 11) and see that it takes its sources from the Jdk related image.

The Maven binaries will be loaded during the building process. We will talk about the other files next (settings…).

Now I bet you are curious on what Linux version the Jdk image is built upon ?

AdoptOpenJdk docker image & linux

Let’s review a bit the Dockerfile of this prebuilt distribution offered by the open source community : AdoptOpenJDK

FROM ubuntu:18.04


RUN apt-get update \

Ubuntu, yes you are right !

If you are used to Docker, you might say : why not using an Alpine version of Linux to reduce the size of the final image ?

Base linux distro for our image

The use of Alpine Linux could have been made if we had shipped it with Glibc. However at the moment, this is not straightforward according to Alpine linux community.

So the use of Ubuntu seems fair but if you follow a few comparison charts, you might pick up another distribution.

 We decided to go with Minideb and the bitnami docker version.

Dockerfile for Maven, jdk8 & jdk11 LTS

Now that we’ve explained how we got here, time for sharing the Dockerfile :

FROM bitnami/minideb:latest

RUN apt-get update \
  && apt-get -y install curl

ADD /opt/jdk/
RUN tar -xzvf /opt/jdk/OpenJDK8U-jdk_x64_linux_hotspot_8u252b09.tar.gz -C /opt/jdk

ADD /opt/jdk/
RUN tar -xzvf /opt/jdk/OpenJDK11U-jdk_x64_linux_hotspot_11.0.7_10.tar.gz -C /opt/jdk

ARG SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0

RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
  && curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
  && echo "${SHA}  /tmp/apache-maven.tar.gz" | sha512sum -c - \
  && tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
  && rm -f /tmp/apache-maven.tar.gz \
  && ln -s /usr/share/maven/bin/mvn /usr/bin/mvn \
  && rm -f /opt/jdk/*.gz

ENV JAVA_HOME /opt/jdk/jdk8u252-b09
ENV MAVEN_HOME /usr/share/maven

COPY /usr/local/bin/
COPY settings.xml /usr/share/maven/ref/
RUN chmod +x /usr/local/bin/

ENTRYPOINT ["/usr/local/bin/"]
CMD ["mvn"]

Notes about what’s going on on the Dockerfile :

  • Set JDK 8 as the default jdk (by defining the JAVA_HOME pointing to this kit)
  • Loading the as AdoptOpenJDK does
  • Loading the settings.xml file for Maven (taken from AdoptOpenJDK as well)

Using a Debian version, we then download the proper jdks and set the entrypoint and default command to run when running the container.

Entrypoint & Settings configuration files

We won’t customize the files used by the image for running Maven but use the ones provided by the version we wish to ship in.

Just download both files by picking the proper image onto official Maven docker hub.

Adopt OpenJDK docker hub
entry point & settings
Entry Point maven

Copy both files respectively under :

  2. settings.xml

Image Build

In order to build the image, simply type the following command :

$ docker build -t jdk8/jdk11 .
You can replace the tag with the one you prefer.
And to test the newly built image :
$ docker run -it –rm jdk8/jdk11 mvn –version
Try now to run the container on a Java project :
$ docker run -it –rm  -v “$(pwd)”:/usr/src/mymaven -w /usr/src/mymaven jdk8/jdk11 mvn clean install
This is just an example of use.
We will talk about a way to fully exploit the potential of the image on a next article featuring a multi module java project 
I hope you learnt something while reading these lines. I would be pleased to study your thoughts about the article.
Keep up !
Image built with success - jdk11 and 8

More external resources

Docker file best practices


And more details about Docker image optimization with Docker Slim

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Want more information?

Related links will be displayed within articles for you to pick up another good spot to get more details about software development, deployment & monitoring.

Stay tuned by following us on Youtube.

%d bloggers like this: