如何在Docker镜像中安装Java 9和Gradle

时间:2018-05-27 07:35:24

标签: java gradle dockerfile

我尝试在Docker镜像中安装OpenJDK9和Gradle 4.5.1。

这是我的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

我收到了一个错误:

  

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

1 个答案:

答案 0 :(得分:1)

ubuntu:latest标记目前为ubuntu:18.04(仿生),仅包含java packagesopenjdk-8-jdk-headlessopenjdk-11-jdk-headless,但不包含openjdk-9-jdk-无头(已经达到end-of-life,至少是为了公开更新)。

openjdk-9-jdk-headless可在ubuntu:16.04(xenial)中找到。

我通过切换到ubuntu:16.04并将wgetunzip添加到要安装的软件包列表中来构建工作,因为它们随后用于下载和解压缩gradle但是默认情况下未安装。

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