Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to install java 8 using docker?

Tags:

docker

java-8

I have a dockerfile that starts with the following line

FROM java:8

I thought this is supposed to pull the image from the docker container registry and install. no?

when I run the java command inside my container I get the following error

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

What is the easiest and the best way to install java 8(openjdk version) using docker?

UPDATE:

RUN apt-get install -y --no-install-recommends software-properties-common
RUN add-apt-repository -y ppa:openjdk-r/ppa
RUN apt-get update
RUN apt-get install -y openjdk-8-jdk
RUN apt-get install -y openjdk-8-jre
RUN update-alternatives --config java
RUN update-alternatives --config javac
like image 708
user1870400 Avatar asked Apr 13 '16 03:04

user1870400


2 Answers

Perhaps you're missing something. 8 tag or 8-jdk are working fine:

$ docker run -ti java:8-jdk
root@ea4ae4cf642e:/# echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

You can also verify by looking at the Dockerfile and see that it indeed defines JAVA_HOME. For example, see java:8 Dockerfile

Also, The simplest form of Dockerfile will, of course, evaluate to the same result. i.e:

FROM java:8-jdk
CMD ["/bin/bash"]

And building in the following way:

$ docker build -t myjava .

Then, executing it:

$ docker run -ti myjava:latest bash
root@3c35f7d2d94a:/# echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64
like image 191
buddy123 Avatar answered Oct 18 '22 20:10

buddy123


Add below setting to your DockerFile to install openjdk 8 in your docker container.

# Install "software-properties-common" (for the "add-apt-repository")
RUN apt-get update && apt-get install -y \
    software-properties-common

# Add the "JAVA" ppa
RUN add-apt-repository -y \
    ppa:webupd8team/java

# Install OpenJDK-8
RUN apt-get update && \
    apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean;

# Fix certificate issues
RUN apt-get update && \
    apt-get install ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f;

# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
like image 33
Atif Hussain Avatar answered Oct 18 '22 19:10

Atif Hussain