Categories: DevOpsDockersJavaWeb

Docker – Quick Java 8 or Java 7 Dev Environment with Dockers

This article represents code samples which could be used to build and start Java 7 or Java 8 Docker containers appropriately based on the needs with a single script and command such as “./startJava.sh -v 7 Java7” for Java 7 and “./startJava.sh -v 8 java8” for Java 8. This would be useful to try and test your Java code against Java 7 and Java 8 versions very easily based on the fact that you could start both Java7 and Java8 containers simultaneously and, run your code in these containers at the same time for testing. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • Java 7 Dev Environment Dockerfile
  • Java 8 Dev Environment Dockerfile
  • Single Shell Script to Start Java 7 or Java 8 Development Environment


Java 7 Dev Environment Dockerfile

Following is dockerfile to build Java 7 image. Save the file as java7.df for the code example (shell script startJava.sh) to work.

FROM centos:centos6

RUN mkdir /opt/jdk
RUN cd /opt

RUN yum -y install wget tar

RUN wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz

RUN tar -zxf jdk-7u79-linux-x64.tar.gz -C /opt/jdk

RUN update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.7.0_79/bin/java 100
RUN update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.7.0_79/bin/javac 100

Java 8 Dev Environment Dockerfile

Following is dockerfile to build Java8 image. Save the file as java8.df for the code example (shell script startJava.sh) to work.

FROM centos:centos6

RUN mkdir /opt/jdk
RUN cd /opt

RUN yum -y install wget tar

RUN wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u5-b13/jdk-8u5-linux-x64.tar.gz

RUN tar -zxf jdk-8u5-linux-x64.tar.gz -C /opt/jdk

RUN update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_05/bin/java 100
RUN update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_05/bin/javac 100

 

Single Shell Script to Start Java 7 or Java 8 Dev Environment

Save the following script as startJava.sh. Save the above two dockerfiles as java7.df and java8.df in the same folder as the script below. Execute the command such as “./startJava.sh -v 7 java7” to start a Java 7 container with name as “java7” and, “./startJava.sh -v 8 java8” to start the Java 8 Container with name as “java8”. You could then access your source code on your m/c as the volume is mounted appropriately in the script below (/c/Users:/mnt/Users).

#!/bin/sh

if [ $# == 0 ]; then
  echo "This script expecs version and container name as argument. Example: ./startJava.sh -v 7 java"
  exit 100
fi

if [ $1 == '-v' ]; then
  expr $2 + 1 2> /dev/null
  if [ $? = 0 ]; then
    echo "Version: $2"
  else
    echo "Version can only be numeric. Value supported are 7, 8"
    exit 100
  fi
else
  echo "This script expecs version and container name as argument. Example: ./startJava.sh -v 7 java"
  exit 100
fi

docker stop $3;docker rm $3

java_image=""
java_df=""
if [ $2 == 8 ]; then
  java_image="java8_base"#!/bin/sh

if [ $# == 0 ]; then
  echo "This script expecs version and container name as argument. Example: ./startJava.sh -v 7 java"
  exit 100
fi

if [ $1 == '-v' ]; then
  expr $2 + 1 2> /dev/null
  if [ $? = 0 ]; then
    echo "Version: $2"
  else
    echo "Version can only be numeric. Value supported are 7, 8"
    exit 100
  fi
else
  echo "This script expecs version and container name as argument. Example: ./startJava.sh -v 7 java"
  exit 100
fi

docker stop $3;docker rm $3

java_image=""
java_df=""
if [ $2 == 8 ]; then
  java_image="java8_base"
  java_df="java8.df"
else
  if [ $2 == 7 ]; then
    java_image="java7_base"
    java_df="java7.df"
  else
    echo "This script only supports Java containers for version 7 or 8. Please try again!"
    exit 100
  fi
fi

# Build Java image if it does not exists
#
if [ `docker images $java_image | wc -l` -lt 2 ]; then
  echo "Docker Image $java_image do not exist..."
  echo "Builing docker image $java_image"
  if [ -f $java_df ]; then
    docker build -t $java_image -f $java_df .
  else
    echo "Can't find Dockerfile $java_df in the current location"
    exit 200
  fi
fi

docker run --privileged=true -ti -dP --name $3 -v /c/Users:/mnt/Users $java_image /bin/bash
docker exec -ti $3 /bin/bash
  java_df="java8.df"
else
  if [ $2 == 7 ]; then
    java_image="java7_base"
    java_df="java7.df"
  else
    echo "This script only supports Java containers for version 7 or 8. Please try again!"
    exit 100
  fi
fi

# Build Java image if it does not exists
#
if [ `docker images $java_image | wc -l` -lt 2 ]; then
  echo "Docker Image $java_image do not exist..."
  echo "Builing docker image $java_image"
  if [ -f $java_df ]; then
    docker build -t $java_image -f $java_df .
  else
    echo "Can't find Dockerfile $java_df in the current location"
    exit 200
  fi
fi

docker run --privileged=true -ti -dP --name $3 -v /c/Users:/mnt/Users $java_image /bin/bash
docker exec -ti $3 /bin/bash


Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

3 weeks ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

4 weeks ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

1 month ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

1 month ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

1 month ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

1 month ago