如何在Docker容器中的Java 8和Java 11之间切换
我正在使用安装两个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行应用程序时,它将仅使用一个JRE或另一个JRE。图像中无需将未使用的JRE放在图像中。
您可以使用dockerfile 应该与图像一起建造什么。这似乎是
运行
命令作为普通环境变量。在两个JRE版本中,大多数安装逻辑都相同,这对此有所帮助。我可能会写的第一件事是一个独立的shell脚本来进行安装。在您喜欢的IDE中进行编辑并在容器外进行测试会更容易。
然后,在Dockerfile中,您需要设置一个
arg
以接受版本设置,copy
在脚本中,run
it。现在,您可以正常构建和运行容器,它将仅包含Java 11
,但您还可以构建在Java 8上构建的替代图像,
这为您节省了尝试找出安装路径的麻烦在启动时,为您提供了一个较小的图像,该图像不包含您不使用的整个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 toRUN
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.
Then in the Dockerfile, you need to set an
ARG
to accept the version setting,COPY
in the script, andRUN
it.Now you can build and run your container as normal, and it will only contain Java 11
But you can also build an alternate image built on Java 8
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 anENV
is hard, but you could do it in an entrypoint wrapper script based on whatjava -version
returns.