Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set JAVA_HOME of docker container

My docker container requires JAVA_HOME to be set. I have added it to the Dockerfile as below

ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/jre

However, this is a hardcoding of the value. Is there a way I can get this value dynamically from the image itself

like image 331
Pranav Kapoor Avatar asked Apr 27 '17 10:04

Pranav Kapoor


2 Answers

Set JAVA_HOME in docker container

Default Docker file of the official image is Dockerfile

If you still want your own image with Java home set. Add this lines to your Dockerfile

RUN apt-get update && \
    apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/ && \
    rm -rf /var/cache/oracle-jdk8-installer;
    
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME

Create the docker-compose.yml, replace the dynamic path with container updating the environment variable in the environment:

like image 128
Jinna Balu Avatar answered Nov 17 '22 05:11

Jinna Balu


It's possible add the JAVA_HOME in a ENV Dockerfile instruction. But if the Java package is updated you have to change the ENV JAVA_HOME in your Dockerfile.

But I found a method to set the JAVA_HOME automatically without update the Dockerfile.

RUN export JAVA_HOME="$(dirname $(dirname $(readlink -f $(which java))))"

WARNING: This is only valid if you run all the commands in the same RUN a one-liner command using the '&&' operand as Mig82 says in the comments section.

I hope this can help you.

like image 4
A.Villegas Avatar answered Nov 17 '22 05:11

A.Villegas