…continuing from Why not build #OpenJDK 9 using #Docker ? – Part 1 of 2.
I ran into a number of issues and you can see from my commits, I pulled myself out of it, but to run this Dockerfile from the command-line I used this instruction:
$ docker build -t neomatrix369/openjdk9 .
you can also do it using the below if you have not set your permissions:
$ sudo docker build -t neomatrix369/openjdk9 .
and get the below (summarised) output:
Sending build context to Docker daemon 3.072 kB Sending build context to Docker daemon Step 0 : FROM phusion/baseimage:latest ---> 5a14c1498ff4 Step 1 : MAINTAINER Mani Sarkar (from @adoptopenjdk) ---> Using cache ---> 95e30b7f52b9 Step 2 : 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/* ---> Using cache ---> 1ea3bbb15c2d Step 3 : RUN apt-get update ---> Using cache ---> 6c3938f4d23d Step 4 : RUN apt-get install -y mercurial ca-certificates-java build-essential ---> Using cache ---> e3f99b5a3bd3 Step 5 : RUN cd /tmp && hg clone http://hg.openjdk.java.net/jdk9/jdk9 openjdk9 && cd openjdk9 && sh ./get_source.sh ---> Using cache ---> 26cfaf16b9fa Step 6 : 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 ---> Using cache ---> 696889250fed Step 7 : RUN tar zxvf jdk-8u45-linux-x64.tar.gz -C /opt ---> Using cache ---> c25cc9201c1b Step 8 : RUN cd /tmp/openjdk9 && bash ./configure --with-cacerts-file=/etc/ssl/certs/java/cacerts --with-boot-jdk=/opt/jdk1.8.0_45 ---> Using cache ---> 4e425de379e6 Step 9 : RUN cd /tmp/openjdk9 && make clean images ---> Using cache ---> 2d9e17c870be Step 10 : RUN cd /tmp/openjdk9 && cp -a build/linux-x86_64-normal-server-release/images/jdk /opt/openjdk9 ---> Using cache ---> 9250fac9b500 Step 11 : RUN cd /tmp/openjdk9 && find /opt/openjdk9 -type f -exec chmod a+r {} + && find /opt/openjdk9 -type d -exec chmod a+rx {} + ---> Using cache ---> d0c597d045d4 Step 12 : ENV PATH /opt/openjdk9/bin:$PATH ---> Using cache ---> 3965c3e47855 Step 13 : ENV JAVA_HOME /opt/openjdk9 ---> Using cache ---> 5877e8efd939 Successfully built 5877e8efd939
The above action creates an image which is stored in your local repository (use docker images to enlist the images in the repo). If you want to load the image into a container, and access the files it has built or see anything else, do the below:
$ sudo docker run -it --name openjdk9 neomatrix369/openjdk9 /bin/bash
this will take you to a bash prompt into the container and you can run any of your linux commands and access the file system.
Explaining docker run
$ sudo docker run -it --name openjdk9 neomatrix369/openjdk9 java -version
will show you this
openjdk version "1.9.0-internal" OpenJDK Runtime Environment (build 1.9.0-internal-_2015_06_04_06_46-b00) OpenJDK 64-Bit Server VM (build 1.9.0-internal-_2015_06_04_06_46-b00, mixed mode)
Here’s a breakdown of the docker run command:
docker run |
The command to create and start a new Docker container. |
-it |
To run in interactive mode, so you can see the after running the container. |
neomatrix369/openjdk9 |
This is a reference to the image tag by name (which we created above). |
java -version |
Runs the java command asking its version, inside the containing, which is assisted by the two environment variables PATH and JAVA_HOME which was set in the Dockerfile above. |
Footnotes
You might have noticed I grouped very specific instructions with each step, especially the RUN commands, its because, each time I got one of these wrong, it would re-execute the step again, including the steps that ran fine and didn’t need re-executing. Not only is this unnecessary its not using our resources efficiently which is what Docker brings us. So any addition, edition or deletion to any step will only result in that step being executed, and not the other steps that are fine.
So one of the best practises is to keep the steps granular enough and pre-load files and data beforehand and give it to docker. It has amazing caching and archiving mechanisms built in.
Save our work
As we know if we do not save the container into the image, our changes are lost.
If I didn’t use the docker build command I used earlier I could have, after the build process was completed and image created, used the below command:
$ sudo docker commit [sha of the image] neomatrix369/openjdk9
Sharing your docker image on Docker hub
Once you are happy with your changes, and want to share it with community at large, do the below:
$ sudo docker push neomatrix369/openjdk9
and you will see these depending on which of your layers have been found in the repo and which ones are new (this one is an example snapshot of the process):
The push refers to a repository [neomatrix369/openjdk9] (len: 1) 5877e8efd939: Image already exists 3965c3e47855: Image already exists d0c597d045d4: Image already exists 9250fac9b500: Image already exists 2d9e17c870be: Buffering to Disk . . .
There is plenty of room for development and improvement of this Docker script. So happy hacking and would love to hear your feedback or contributions from you.
BIG Thanks
Big thanks to the below two who proof-read my post and added value to it, whilst enjoying the #Software #Craftsmanship developer community (organised and supported by @LSCC):
Oliver Nautsch – @ollispieps (JUG Switzerland)
Amir Bazazi (@Codurance) – @amirbazazi
Special thanks to Roberto Cortez (@radcortez) for your Docker posts, these inspired and helped me write my first Docker post.
Resources
[1] Docker
[2] Get into Docker – A Guide for Total Newbies
[3] Docker for Total Newbies Part 2: Distribute Your Applications with Docker Images
[4] Docker posts on Voxxed
[5] OpenJDK
[6] Building OpenJDK
[7] Building OpenJDK on Linux, MacOs and Windows
[8] Virtual Machines (OpenJDK)
[9] Build your own OpenJDK
[10] Vagrant script (OpenJDK)
[11] YOUR DOCKER IMAGE MIGHT BE BROKEN without you knowing it
[12] Dockerfile on github
[13] Adopt OpenJDK: Getting Started Kit
[14] London Java Community