Set JAVA_HOME on ubuntu docker Container

Felix Gondwe
Nov 7 · 2 min read

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 -version

It will show something similar to below

set JAVA_HOME on ubuntu docker container
set JAVA_HOME on ubuntu docker container

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 java

In my case the following showed up

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Step 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-jdk
ENV 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_HOME

That’s it! 😃Now go out share and spread the ❤️

Felix Gondwe

Written by

Software Developer | Casual Photographer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade