Introduction

I think I have joined the Docker [1] party a bit late – but that means that by now, everyone is likely familiar with what Docker is and all the other basic fundamentals. However, if you are still interested, please check these posts Get into Docker – A Guide for Total Newbies [2] and Docker for Total Newbies Part 2: Distribute Your Applications with Docker Images [3]. And if you still want to know more about this widely spoken topic, check out these Docker posts on Voxxed [4].

Why ?

Since everyone has been doing some sort of provisioning or spinning up of dev or pre-prod or test environments using Docker [1] I decided to do the same but with my favourite project; OpenJDK [5].

So far you can natively build OpenJDK [6] across Linux, MacOs and Windows [7], or do the same things via virtual machines or vagrant instances, see more on then via these resources Virtual Machines, [8] Build your own OpenJDK [9] and this vagrant script [10]. These are all part of the Adopt OpenJDK initiative lead by London Java Community [14] and supported by JUGs all over the world.

Requirements

This post is for those using Linux distributions (this one was created on Ubuntu 14.04). Linux, MacOS and Windows users please refer to Docker‘s Linux, MacOS and Windows instructions respectively.

*** Please read the above links and ensure Docker works for you for the above platforms – try out tutorials or steps proving that Docker works for you before proceeding further. ***

Building OpenJDK 9 using Docker

Now I will show you how to do the same things as mentioned above using Docker.

So I read the first two resource I shared so far (and wrote the last ones). So lets get started, and I’m going to walk you through what the Docker file looks like, as I take you through each section of the Docker code:

You have noticed unlike everyone else I have chosen a different OS image i.e. phusion/baseimage, why? Read YOUR DOCKER IMAGE MIGHT BE BROKEN without you knowing it [11], to learn more about it.

FROM phusion/baseimage:latest

Each of the RUN steps below when executed becomes a Docker layer in isolation and gets assigned a SHA like this i.e. 95e30b7f52b9.

RUN 
  apt-get update && 
  apt-get install -y 
    libxt-dev zip pkg-config libX11-dev libxext-dev 
    libxrender-dev libxtst-dev libasound2-dev libcups2-dev libfreetype6-dev && 
  rm -rf /var/lib/apt/lists/*

The base image is updated and a number of dependencies are installed i.e. Mercurial (hg) and build-essential.

RUN 
  apt-get update && 
  apt-get install -y mercurial ca-certificates-java build-essential

Clone the OpenJDK 9 sources and download the latest sources from mercurial. You will notice that each of these steps are prefixed by this line cd /tmp &&, this is because each instruction is run in its own layer, as if it does not remember where it was when the previous instruction was run. Nothing to worry about, all your changes are still intact in the container.

RUN 
  cd /tmp && 
  hg clone http://hg.openjdk.java.net/jdk9/jdk9 openjdk9 && 
  cd openjdk9 && 
  sh ./get_source.sh

Install only what you need when you need them, see below I downloaded wget and then the jdk binary. I also learnt how to use wget to pass the necessary params and headers to make the server give us the binary we request. Finally un-tar the file using the famous tar command.

RUN 
  apt-get install -y wget && 
  wget --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie"  
http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz

RUN 
  tar zxvf jdk-8u45-linux-x64.tar.gz -C /opt

Run configure with the famous –with-boot-jdk=/opt/jdk1.8.0_45 to set the bootstrap jdk to point to jdk1.8.0_45.

RUN 
  cd /tmp/openjdk9 && 
  bash ./configure --with-cacerts-file=/etc/ssl/certs/java/cacerts --with-boot-jdk=/opt/jdk1.8.0_45

Now run the most important command:

RUN   
  cd /tmp/openjdk9 && 
  make clean images

Once the build is successful, the artefacts i.e. jdk and jre images are created in the build folder.

RUN   
  cd /tmp/openjdk9 && 
  cp -a build/linux-x86_64-normal-server-release/images/jdk 
    /opt/openjdk9

Below are some chmod ceremonies across the files and directories in the openjdk9 folder.

RUN   
  cd /tmp/openjdk9 && 
  find /opt/openjdk9 -type f -exec chmod a+r {} + && 
  find /opt/openjdk9 -type d -exec chmod a+rx {} +

Two environment variable i.e. PATH and JAVA_HOME are created with the respective values assigned to them.

ENV PATH /opt/openjdk9/bin:$PATH
ENV JAVA_HOME /opt/openjdk9

You can find the entire source for the entire Dockerfile on github [12].

…more of this in the next post.

Build OpenJDK 9 using Docker – Part 1

About The Author
- Mani Sarkar is a passionate Java/JVM developer and is an advocate to many projects (mainly F/OSS). He is also a strong supporter of software craftsmanship principles like BDD and TDD, and is a practitioner of techniques such as pair-programming and code reviews. Enjoys refactoring, writing tests and helping others revive their legacy code-base, using various tools and home-grown methods. He is an active member of a number of developer communities, and shares his passion for open source technologies by blogging and presenting at conferences and hands-on workshops in the UK and other parts of Europe.

2 Comments

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>