Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing maven in a docker build overrides JAVA 8 with JAVA 7(!)

It appears that maven overrides Java 8 with Java 7. Consider the following Dockerfile:

FROM java:8
RUN java -version && ls -l /usr/bin/java    
RUN apt-get update -y && apt-get install maven -y
RUN java -version && ls -l /usr/bin/java    

Line two will report the java version is 1.8 but line 4 will report java version is 1.7. In both cases the /usr/bin/java symlink points to /etc/alternatives/java

Besides re-installing Java 8 (which is why I started with Java:8 in the first place), how can I undo these side-effects of installing maven when building a docker image?

like image 227
Tevya Avatar asked Sep 16 '15 17:09

Tevya


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.

Is Docker same as Maven?

Docker belongs to "Virtual Machine Platforms & Containers" category of the tech stack, while Apache Maven can be primarily classified under "Java Build Tools". Some of the features offered by Docker are: Integrated developer tools. open, portable images.


2 Answers

I found a minimal-delta solution although the point about not using apt-get for maven installs is noted. Here is the solution as the code

FROM java:8

# preserve Java 8  from the maven install.
RUN mv /etc/alternatives/java /etc/alternatives/java8
RUN apt-get update -y && apt-get install maven -y

# Restore Java 8
RUN mv -f /etc/alternatives/java8 /etc/alternatives/java
RUN ls -l /usr/bin/java && java -version

Obviously, the last line is unnecessary but does confirm that the result is java 8.

like image 54
Tevya Avatar answered Oct 16 '22 17:10

Tevya


Your problem isn't Maven, it's some dumb decision made by the person who packaged Maven into a .deb for APT. Do not use Maven from a .deb. The Apache Maven project doesn't make these, doesn't know what's in them, and does not support them very much. Download the genuine tar.gz from maven.apache.org, it will happily work with whatever version of Java you've got.

like image 45
bmargulies Avatar answered Oct 16 '22 16:10

bmargulies