17

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 
1
  • Don't you mean 'apt-get -y install'?
    – devssh
    CommentedMay 16, 2019 at 10:42

3 Answers 3

11

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 
6
  • FROM java:8-jdk (I changed it to this but no effect) CMD ["/bin/bash"] (I dont have this but why do I need this as far as installation of java 8 is concerned?)CommentedApr 13, 2016 at 7:05
  • 1
    @user1870400 How does your Dockerfile look? What are you trying to do? Having your container based on java image only means you'll have java base image preconfigured with all java dependencies. The rest depends on what you're trying to do. FROM java:8-jdk will give you java installed in your image
    – buddy123
    CommentedApr 13, 2016 at 7:13
  • my docker file consists of one line FROM java:8-jdk and when I do docker build -t dockerfile . it builds fine and then when I do docker run -i -t --name hello dockerfile I am inside the container and now when I type java or javac or echo $JAVA_HOME none of them workCommentedApr 13, 2016 at 7:17
  • @user1870400 This is working OK for me. What is your docker version?
    – buddy123
    CommentedApr 13, 2016 at 7:32
  • Docker version 1.10.3CommentedApr 13, 2016 at 7:41
5

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 
1
  • 5
    This is wrong, that repo is discontinued. Read here: launchpad.net/~webupd8team/+archive/ubuntu/java . Citation: The new Oracle Technology Network License Agreement for Oracle Java SE is substantially different from prior Oracle JDK licenses. The new license permits certain uses, such as personal use and development use, at no cost -- but other uses authorized under prior Oracle JDK licenses may no longer be available. Please review the terms carefully before downloading and using this product.
    – Marecky
    CommentedJun 2, 2020 at 11:47
1

You can use sdkman to install java8

Docker file content as given below:

RUN apt-get update && apt-get install -y curl zip unzip RUN curl -s 'https://get.sdkman.io' | bash RUN /bin/bash -c "source $HOME/.sdkman/bin/sdkman-init.sh; sdk version; sdk install java 8.0.302-open; sdk install maven 3.8.6" 

Use following command before using it in a shell script

source "$HOME/.sdkman/bin/sdkman-init.sh" export PATH=$PATH:/root/.sdkman/candidates/java/current/ 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.