如何在Docker容器中的Java 8和Java 11之间切换

发布于 2025-01-23 12:06:39 字数 2584 浏览 0 评论 0原文

我正在使用安装两个Java版本的Dockerfile(Java 8和Java 11)。我想将Java 11设置为默认版本。根据需要,它从Java 11转换为Java 8。为此,我从私有基础图像中构建了Docker映像,并安装了两个版本并为Java 11提供了环境,我创建了一个Shell脚本,该脚本将Java版本从Java 11转换为11 Java 8在运行容器中。这种方法失败了。请让我知道解决此问题的可能性。

Dockerfile:

FROM private=base-image
ARG JAVA_VERSION=11.0.12.0
ARG JAVA_ARCHIVE_NAME=java-certified-jdk_x64_linux_11.0.12.0.bin
RUN curl {java-resource}/cloud/runtimes/java/${JAVA_VERSION}/linux/x86_64/${JAVA_ARCHIVE_NAME} -o /tmp/java-sdk-${JAVA_VERSION}.bin && \
chmod +x /tmp/java-sdk-${JAVA_VERSION}.bin &&\
echo "INSTALLER_UI=silent" > /tmp/java-installer.properties && \
echo "LICENSE_ACCEPTED=TRUE" >> /tmp/java-installer.properties && \
echo "USER_INSTALL_DIR=/opt/java/java-certified-11-jdk" >> /tmp/java-installer.properties && \
/tmp/java-java-sdk-${JAVA_VERSION}.bin -i silent -f /tmp/java-installer.properties && \
ln -sf /opt/java/java-certified-11-jdk/bin/* /usr/local/bin/  && \
rm /tmp/java-java-sdk-${JAVA_VERSION}.bin && \
rm /tmp/java-installer.properties

ARG JAVA8_VERSION=8.0.6.35
ARG JAVA8_ARCHIVE_NAME=java-sdk-8.0-6.35-x86_64-archive.bin
RUN curl https://public.dhe.java.com/javadl/export/pub/systems/cloud/runtimes/java/${JAVA8_VERSION}/linux/x86_64/${JAVA8_ARCHIVE_NAME} -o /tmp/java-java-sdk-${JAVA8_VERSION}.bin && \
chmod +x /tmp/java-java-sdk-${JAVA8_VERSION}.bin &&\
echo "INSTALLER_UI=silent" > /tmp/java-installer.properties && \
echo "LICENSE_ACCEPTED=TRUE" >> /tmp/java-installer.properties && \
echo "USER_INSTALL_DIR=/opt/java/java" >> /tmp/java-installer.properties && \
/tmp/java-sdk-${JAVA8_VERSION}.bin -i silent -f /tmp/java-installer.properties && \
rm /tmp/java-java-sdk-${JAVA8_VERSION}.bin && \
rm /tmp/java-installer.properties
ENV PATH=$PATH:/opt/java/java-certified-11-jdk/bin \
    JAVA_HOME=/opt/java/java-certified-11-jdk \
    java_JAVA_OPTIONS="-XX:+UseContainerSupport"

容器内的外壳逻辑:

#!/bin/bash
TARGET_JDK=JAVA8
if [ $TARGET_JDK == JAVA8 ]; then
   JAVA_PATH="/opt/xxx/java"
   echo "${JAVA_PATH}"
   export PATH=$PATH:${JAVA_PATH}/bin
   export JAVA_HOME=${JAVA_PATH}
   export xxx_JAVA_OPTIONS="-XX:+UseContainerSupport"
else
   JAVA_PATH="/opt/xxx/xxx-yyy-certified-11-jdk"
   echo "${JAVA_PATH}"
   export PATH=$PATH:${JAVA_PATH}/bin
   export JAVA_HOME=${JAVA_PATH}
   export xxx_JAVA_OPTIONS="-XX:+UseContainerSupport"
fi

I am using Dockerfile which installs two Java versions (Java 8 and Java 11). I want to set Java 11 as default version. As per the need it switch from Java 11 to Java 8. For this, I built Docker image from private base-image and installed both versions and provided environment for the Java 11 and I created a shell script which switches Java version from Java 11 to Java 8 during running container. This approach is failing. Please let me know any possibility to resolve this issue.

Dockerfile :

FROM private=base-image
ARG JAVA_VERSION=11.0.12.0
ARG JAVA_ARCHIVE_NAME=java-certified-jdk_x64_linux_11.0.12.0.bin
RUN curl {java-resource}/cloud/runtimes/java/${JAVA_VERSION}/linux/x86_64/${JAVA_ARCHIVE_NAME} -o /tmp/java-sdk-${JAVA_VERSION}.bin && \
chmod +x /tmp/java-sdk-${JAVA_VERSION}.bin &&\
echo "INSTALLER_UI=silent" > /tmp/java-installer.properties && \
echo "LICENSE_ACCEPTED=TRUE" >> /tmp/java-installer.properties && \
echo "USER_INSTALL_DIR=/opt/java/java-certified-11-jdk" >> /tmp/java-installer.properties && \
/tmp/java-java-sdk-${JAVA_VERSION}.bin -i silent -f /tmp/java-installer.properties && \
ln -sf /opt/java/java-certified-11-jdk/bin/* /usr/local/bin/  && \
rm /tmp/java-java-sdk-${JAVA_VERSION}.bin && \
rm /tmp/java-installer.properties

ARG JAVA8_VERSION=8.0.6.35
ARG JAVA8_ARCHIVE_NAME=java-sdk-8.0-6.35-x86_64-archive.bin
RUN curl https://public.dhe.java.com/javadl/export/pub/systems/cloud/runtimes/java/${JAVA8_VERSION}/linux/x86_64/${JAVA8_ARCHIVE_NAME} -o /tmp/java-java-sdk-${JAVA8_VERSION}.bin && \
chmod +x /tmp/java-java-sdk-${JAVA8_VERSION}.bin &&\
echo "INSTALLER_UI=silent" > /tmp/java-installer.properties && \
echo "LICENSE_ACCEPTED=TRUE" >> /tmp/java-installer.properties && \
echo "USER_INSTALL_DIR=/opt/java/java" >> /tmp/java-installer.properties && \
/tmp/java-sdk-${JAVA8_VERSION}.bin -i silent -f /tmp/java-installer.properties && \
rm /tmp/java-java-sdk-${JAVA8_VERSION}.bin && \
rm /tmp/java-installer.properties
ENV PATH=$PATH:/opt/java/java-certified-11-jdk/bin \
    JAVA_HOME=/opt/java/java-certified-11-jdk \
    java_JAVA_OPTIONS="-XX:+UseContainerSupport"

Shell logic inside the container:

#!/bin/bash
TARGET_JDK=JAVA8
if [ $TARGET_JDK == JAVA8 ]; then
   JAVA_PATH="/opt/xxx/java"
   echo "${JAVA_PATH}"
   export PATH=$PATH:${JAVA_PATH}/bin
   export JAVA_HOME=${JAVA_PATH}
   export xxx_JAVA_OPTIONS="-XX:+UseContainerSupport"
else
   JAVA_PATH="/opt/xxx/xxx-yyy-certified-11-jdk"
   echo "${JAVA_PATH}"
   export PATH=$PATH:${JAVA_PATH}/bin
   export JAVA_HOME=${JAVA_PATH}
   export xxx_JAVA_OPTIONS="-XX:+UseContainerSupport"
fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2025-01-30 12:06:40

运行应用程序时,它将仅使用一个JRE或另一个JRE。图像中无需将未使用的JRE放在图像中。

您可以使用dockerfile 应该与图像一起建造什么。这似乎是运行命令作为普通环境变量。在两个JRE版本中,大多数安装逻辑都相同,这对此有所帮助。

我可能会写的第一件事是一个独立的shell脚本来进行安装。在您喜欢的IDE中进行编辑并在容器外进行测试会更容易。

#!/bin/sh

if [ "$1" = 8 ]; then
  JAVA_VERSION=8.0.6.35
  JAVA_ARCHIVE_NAME=java-sdk-8.0-6.35-x86_64-archive.bin
elsif [ "$1" = 11 ]; then
  JAVA_VERSION=11.0.12.0
  JAVA_ARCHIVE_NAME=java-certified-jdk_x64_linux_11.0.12.0.bin
else
  echo >&2 <<EOF
Usage: install-java.sh [8|11]
EOF
  exit 1
fi

curl {java-resource}/cloud/runtimes/java/${JAVA_VERSION}/linux/x86_64/${JAVA_ARCHIVE_NAME} -o /tmp/java-sdk-${JAVA_VERSION}.bin
chmod +x /tmp/java-sdk-${JAVA_VERSION}.bin

cat >/tmp/java-installer.properties <<EOF
INSTALLER_UI=silent
LICENSE_ACCEPTED=TRUE
USER_INSTALL_DIR=/opt/java
EOF

/tmp/java-java-sdk-${JAVA_VERSION}.bin -i silent -f /tmp/java-installer.properties
rm /tmp/java-java-sdk-${JAVA_VERSION}.bin
rm /tmp/java-installer.properties

然后,在Dockerfile中,您需要设置一个arg以接受版本设置,copy在脚本中,run it。

COPY install-java.sh ./
ARG java_version=11
RUN ./install-java.sh ${java_version}
ENV PATH=/opt/java/bin:$PATH

现在,您可以正常构建和运行容器,它将仅包含Java 11

docker build -t myapp-java11 .
docker run --rm myapp-java11 java -version
docker run myapp-java11

,但您还可以构建在Java 8上构建的替代图像,

docker build --build-arg java_version=8 myapp-java8 .
docker run --rm myapp-java8 java -version
docker run myapp-java8

这为您节省了尝试找出安装路径的麻烦在启动时,为您提供了一个较小的图像,该图像不包含您不使用的整个JRE。

该设置不做的一件事是添加+XX:ContainSupport设置,但是在最近的旧Java 8的最新构建中,不必必要/code>很难,但是您可以根据java -version返回的入门点包装器脚本进行操作。

When you run your application, it will only use one JRE or the other. There's not a need to have the unused JRE in the image.

You can use a Dockerfile ARG to specify what JRE the image should be built with. This appears to RUN commands as a normal environment variable. This is helped by most of your installation logic being the same across both JRE versions.

The first thing I might write is a standalone shell script to do the installation. This will be a little easier to edit in your favorite IDE, and to test outside the container.

#!/bin/sh

if [ "$1" = 8 ]; then
  JAVA_VERSION=8.0.6.35
  JAVA_ARCHIVE_NAME=java-sdk-8.0-6.35-x86_64-archive.bin
elsif [ "$1" = 11 ]; then
  JAVA_VERSION=11.0.12.0
  JAVA_ARCHIVE_NAME=java-certified-jdk_x64_linux_11.0.12.0.bin
else
  echo >&2 <<EOF
Usage: install-java.sh [8|11]
EOF
  exit 1
fi

curl {java-resource}/cloud/runtimes/java/${JAVA_VERSION}/linux/x86_64/${JAVA_ARCHIVE_NAME} -o /tmp/java-sdk-${JAVA_VERSION}.bin
chmod +x /tmp/java-sdk-${JAVA_VERSION}.bin

cat >/tmp/java-installer.properties <<EOF
INSTALLER_UI=silent
LICENSE_ACCEPTED=TRUE
USER_INSTALL_DIR=/opt/java
EOF

/tmp/java-java-sdk-${JAVA_VERSION}.bin -i silent -f /tmp/java-installer.properties
rm /tmp/java-java-sdk-${JAVA_VERSION}.bin
rm /tmp/java-installer.properties

Then in the Dockerfile, you need to set an ARG to accept the version setting, COPY in the script, and RUN it.

COPY install-java.sh ./
ARG java_version=11
RUN ./install-java.sh ${java_version}
ENV PATH=/opt/java/bin:$PATH

Now you can build and run your container as normal, and it will only contain Java 11

docker build -t myapp-java11 .
docker run --rm myapp-java11 java -version
docker run myapp-java11

But you can also build an alternate image built on Java 8

docker build --build-arg java_version=8 myapp-java8 .
docker run --rm myapp-java8 java -version
docker run myapp-java8

This saves you the trouble of trying to figure out the installation path at startup time, and gives you a smaller image that doesn't contain an entire JRE you're not using.

The one thing this setup doesn't do is add the +XX:ContainerSupport setting, but it shouldn't be necessary on recent builds of even the older Java 8. Conditionally setting an ENV is hard, but you could do it in an entrypoint wrapper script based on what java -version returns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文