Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 487
Ankur Sawhney Avatar asked Nov 15 '17 14:11

Ankur Sawhney


People also ask

How do I specify Openjdk in Dockerfile?

You can use the Java 11 image from hub.docker.com by typing the command :docker pull openjdk:tag on your machines terminal, where the tag is version of your intended java version. Or you can simply specify the image on your Dockerfile where FROM attribute must be the version of java.

What is the Openjdk Docker image based on?

openjdk:<version>-alpine This image is based on the popular Alpine Linux project, available in the alpine official image.

What is Openjdk slim?

openjdk:<version>-slimIt only contains the minimal packages needed to run Java. Unless you are working in an environment where only the openjdk image will be deployed and you have space constraints, we highly recommend using the default image of this repository. Follow this answer to receive notifications.


2 Answers

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
like image 147
stacksonstacks Avatar answered Sep 30 '22 01:09

stacksonstacks


You can use Docker multistage build to achieve that. You would basically copy the java installation from one image into another image. Here is what the dockerfile might look like:

FROM openjdk:7-alpine as java7

FROM openjdk:8-alpine
COPY --from=java7 /usr/lib/jvm/java-1.7-openjdk /usr/lib/jvm/java-1.7-openjdk

Now you will have both java installations with the jdk7 installation being under /usr/lib/jvm/java-1.7-openjdk

like image 22
yamenk Avatar answered Sep 30 '22 01:09

yamenk