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
Termininja
  • 6,620
  • 12
  • 48
  • 49
user1870400
  • 6,028
  • 13
  • 54
  • 115

3 Answers3

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
buddy123
  • 5,679
  • 10
  • 47
  • 73
  • 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?) – user1870400 Apr 13 '16 at 07: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 Apr 13 '16 at 07: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 work – user1870400 Apr 13 '16 at 07:17
  • @user1870400 This is working OK for me. What is your docker version? – buddy123 Apr 13 '16 at 07:32
  • Docker version 1.10.3 – user1870400 Apr 13 '16 at 07:41
  • @user1870400 Could it be you already built this image tag once and its your older version image? try `docker build --no-cache -t dockerfile .` – buddy123 Apr 13 '16 at 07:48
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
Atif Hussain
  • 880
  • 12
  • 19
  • 5
    This is wrong, that repo is discontinued. Read here: https://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 Jun 02 '20 at 11:47
0

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/
silentsudo
  • 6,730
  • 6
  • 39
  • 81