How to install multiple openjdk versions on alpine-based docker container

Ankur Sawhney picture Ankur Sawhney · Nov 15, 2017 · Viewed 15.9k times · Source

I wish to install jdk7 and jdk8 on an alpine container side by side. I would like to pick jdk7 only if an env variable is set.

I've chained FROM openjdk:7-alpine and FROM openjdk:8-alpine, but regardless of their relative order, the latter one overwrites the former. So, I am left with only 1 installation as seen in '/usr/lib/jvm'.

Why I need this:

I need this setup for a slave container for Jenkins. Now, jenkins remoting jar runs ONLY on jdk8 now. So, I need it. Plus, since I am spawning this container for a project which needs jdk7 as default jdk, I need that too.

My Dockerfile: https://github.com/ankurshashcode/docker-slave/blob/alpine/Dockerfile

Answer

stacksonstacks picture stacksonstacks · Nov 16, 2017

You should keep it simple and use one base image.
Use openjdk7 as base image, install openjdk8 as a package. This will overwrite openjdk7 as the default JDK while leaving it in the image.

   # Example Dockerfile
   FROM openjdk:7-alpine
   RUN apk add --no-cache openjdk8

   # Other setup...

Verify

$> java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (IcedTea 3.4.0) (Alpine 8.131.11-r2)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

$> ls /usr/lib/jvm/
default-jvm       java-1.7-openjdk  java-1.8-openjdk