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" 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
Follow him on Twitter and Google+.
Latest posts by Ajitesh Kumar (see all)
- MongoDB – How to Enable Authentication on Mongo Database - August 21, 2017
- MongoDB Command to Evaluate Query Performance - August 20, 2017
- Spring Boot with JSP Pages – Code Example - August 19, 2017