title: Configure JDK for running Maven inside Dockerfile based on tomcat:9
body: <p>I am trying to build a Docker image based on the standard Docker image <a href="https://hub.docker.com/_/tomcat/" rel="nofollow"><code>tomcat:9</code></a> that can also compile Java using Maven:</p>
  
  <p>When I first <code>docker run</code> <code>tomcat:9</code> I get these results:</p>
  
  <pre><code>java -version                       # openjdk version "1.8.0_102"
  javac -version                      # javac: command not found
  echo $JAVA_HOME                     # /usr/lib/jvm/java-8-openjdk-amd64/jre
  dpkg --get-selections | grep \\-jre # openjdk-8-jre-headless:amd64
  dpkg --get-selections | grep \\-jdk # (none)
  update-alternatives --config java   
  # There is only one alternative in link group java (providing /usr/bin/java)
  update-alternatives --config javac  # error: no alternatives for javac
  </code></pre>
  
  <p>When I then <code>docker build</code> a derived Docker image with this <code>Dockerfile</code></p>
  
  <pre><code>FROM tomcat:9
  RUN apt-get update
  RUN apt-get install -y openjdk-7-jdk maven
  </code></pre>
  
  <p>and <code>docker run</code> it I get these results:</p>
  
  <pre><code>java -version                       # java version "1.7.0_111"
  javac -version                      # javac 1.7.0_111
  echo $JAVA_HOME                     # /usr/lib/jvm/java-8-openjdk-amd64/jre
  dpkg --get-selections | grep \\-jre # default-jre, default-jre-headless, openjdk-7-jre:amd64, openjdk-7-jre-headless:amd64, openjdk-8-jre-headless:amd64
  dpkg --get-selections | grep \\-jdk # penjdk-7-jdk:amd64
  update-alternatives --config java   # (3 alternatives 
  # /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java, 
  # /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java, 
  # /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java,
  # with the 1st one selected)
  update-alternatives --config javac  
  # There is only one alternative in link group java (providing /usr/bin/javac)
  </code></pre>
  
  <p>However, if I then <code>RUN mvn package</code> in the same Dockerfile (and in I directory where my <code>pom.xml</code> exists),
  I get this error message:</p>
  
  <pre><code>No compiler is provided in this environment. 
  Perhaps you are running on a JRE rather than a JDK?
  </code></pre>
  
  <p>How can I make Maven aware of the installed JDK (instead of JRE) in this environment?</p>
  
  <p>Is <code>export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64</code> (i.e. changing <code>JAVA_HOME</code> manually) the correct way to go or is there a better way, e.g. in terms of system integrity?</p>