EladS
EladS

Reputation: 29

How to install Java 9 and Gradle in a Docker image

I try to install OpenJDK9 and Gradle 4.5.1 in a Docker image.

This is my Dockerfile:

FROM ubuntu:latest
MAINTAINER Hari Sekhon (https://www.linkedin.com/in/harisekhon)

LABEL Description="Java + Ubuntu (OpenJDK)"

ENV DEBIAN_FRONTEND noninteractive

ARG JAVA_VERSION=9
ARG JAVA_RELEASE=JDK

ENV JAVA_HOME=/usr

RUN bash -c ' \
    set -euxo pipefail && \
    apt-get update && \
    pkg="openjdk-$JAVA_VERSION"; \
    if [ "$JAVA_RELEASE" = "JDK" ]; then \
        pkg="$pkg-jdk-headless"; \
    else \
        pkg="$pkg-jre-headless"; \
    fi; \
    apt-get install -y --no-install-recommends "$pkg" && \
    apt-get clean'


CMD /bin/bash

#install Gradle
RUN wget -q https://services.gradle.org/distributions/gradle-4.5.1-bin.zip \
    && unzip gradle-4.5.1-bin.zip -d /opt \
    && rm gradle-4.5.1-bin.zip

# Set Gradle in the environment variables
ENV GRADLE_HOME /opt/gradle-4.5.1
ENV PATH $PATH:/opt/gradle-4.5.1/bin

And I am getting an error:

ubuntu@automation-ubuntu-17:~/dockerFiles$ The command '/bin/sh -c bash -c ' set -euxo pipefail && apt-get update &&
pkg="openjdk-$JAVA_VERSION"; if [ "$JAVA_RELEASE" = "JDK" ]; then pkg="$pkg-jdk-headless"; else pkg="$pkg-jre-headless";
fi; apt-get install -y --no-install-recommends "$pkg" &&
apt-get clean'' returned a non-zero code: 100

Upvotes: 3

Views: 4714

Answers (1)

codemonkey
codemonkey

Reputation: 3830

The ubuntu:latest tag is currently ubuntu:18.04 (bionic), which only contains the java packages for openjdk-8-jdk-headless and openjdk-11-jdk-headless but not openjdk-9-jdk-headless (which has already reached end-of-life, at least for public updates).

openjdk-9-jdk-headless is available in ubuntu:16.04 (xenial), though.

I got the build working by switching to ubuntu:16.04, and also adding wget and unzip to the list of packages to install as they are subsequently used to download and unpack gradle but are not installed by default.

FROM ubuntu:16.04
MAINTAINER Hari Sekhon (https://www.linkedin.com/in/harisekhon)

LABEL Description="Java + Ubuntu (OpenJDK)"

ENV DEBIAN_FRONTEND noninteractive

ARG JAVA_VERSION=9
ARG JAVA_RELEASE=JDK

ENV JAVA_HOME=/usr

RUN bash -c ' \
    set -euxo pipefail && \
    apt-get update && \
    pkg="openjdk-$JAVA_VERSION"; \
    if [ "$JAVA_RELEASE" = "JDK" ]; then \
        pkg="$pkg-jdk-headless"; \
    else \
        pkg="$pkg-jre-headless"; \
    fi; \
    apt-get install -y --no-install-recommends wget unzip "$pkg" && \
    apt-get clean'


CMD /bin/bash

#install Gradle
RUN wget -q https://services.gradle.org/distributions/gradle-4.5.1-bin.zip \
    && unzip gradle-4.5.1-bin.zip -d /opt \
    && rm gradle-4.5.1-bin.zip

# Set Gradle in the environment variables
ENV GRADLE_HOME /opt/gradle-4.5.1
ENV PATH $PATH:/opt/gradle-4.5.1/bin

Upvotes: 1

Related Questions