Dynamically set JAVA_HOME of docker container

2019-03-05 23:55发布

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

3条回答
Melony?
2楼-- · 2019-03-06 00:11

May be you can do something like this in CMD :

# rpm -qa | grep java-1.8
java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64

and then

#rpm -ql java-1.8.0-openjdk | sed -n '1p' | cut -d/ -f1-5
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64
查看更多
Juvenile、少年°
3楼-- · 2019-03-06 00:25

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
查看更多
爷的心禁止访问
4楼-- · 2019-03-06 00:33

The image built from a Dockerfile is forever static. Its Java location will never change unless rebuilt.

When the image is started as a container, however, anything can happen. If there's any scripts that will edit the Java location during runtime, then this script is probably also where JAVA_HOME should be updated.

If you mean that you want to dynamically build your image using an arbitrary base image with differing java location then this should probably be handled by a build script.

查看更多
登录 后发表回答