Set JAVA_HOME on ubuntu docker Container
This is useful if you’re going to run a docker service or container that requires to know where java is. For example some previous versions of elasticsearch require JAVA_HOME in the environment variables.
$(mac) or #(ubuntu container or comments) at each command should be ignored if you’re copying and pasting, it’s for visuals.
Step 1
# Dockerfile# base image
FROM ubuntu: 16.04# install packages
RUN apt-get update && \
apt-get install -y curl \
wget \
openjdk-8-jdk
Step 2
Then build your container from the docker-file above. Then run the container after it’s built.
$ docker build -t name_of_your_image . #build$ docker run -d name_of_your image #it will 'ssh' automatically
Let’s check if java actually installed
/# java -versionIt will show something similar to below

Step 3
Then we will use update-alternatives utility inside our ubuntu docker container to find java path that we can add to our JAVA_HOME variable. Run the following
/# update-alternatives --list javaIn my case the following showed up
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/javaStep 4
Now time we’ve all been waiting for, add the damn path to docker ENV! Check the updated Dockerfile below
# Dockerfile# base image
FROM ubuntu: 16.04# install packages
RUN apt-get update && \
apt-get install -y curl \
wget \
openjdk-8-jdkENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Step 5
Rebuild your image like we did already in step 2, then run it. If you run the following command in your container terminal, you should be able to see the JAVA_HOME path
/# echo $JAVA_HOMEThat’s it! 😃Now go out share and spread the ❤️
