java 如何在基于 alpine 的 docker 容器上安装多个 openjdk 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47310526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install multiple openjdk versions on alpine-based docker container
提问by Ankur Sawhney
I wish to install jdk7 and jdk8 on an alpine container side by side. I would like to pick jdk7 only if an env variable is set.
我希望在一个高山容器上并排安装 jdk7 和 jdk8。仅当设置了 env 变量时,我才想选择 jdk7。
I've chained FROM openjdk:7-alpine
and FROM openjdk:8-alpine
, but regardless of their relative order, the latter one overwrites the former. So, I am left with only 1 installation as seen in '/usr/lib/jvm'
.
我已经链接了FROM openjdk:7-alpine
and FROM openjdk:8-alpine
,但无论它们的相对顺序如何,后者都会覆盖前者。所以,我只剩下 1 个安装,如'/usr/lib/jvm'
.
Why I need this:
为什么我需要这个:
I need this setup for a slave container for Jenkins. Now, jenkins remoting jar runs ONLY on jdk8 now. So, I need it. Plus, since I am spawning this container for a project which needs jdk7 as default jdk, I need that too.
我需要为 Jenkins 的从属容器进行此设置。现在,jenkins 远程处理 jar 现在仅在 jdk8 上运行。所以,我需要它。另外,由于我正在为需要 jdk7 作为默认 jdk 的项目生成此容器,因此我也需要它。
My Dockerfile:https://github.com/ankurshashcode/docker-slave/blob/alpine/Dockerfile
我的 Dockerfile:https : //github.com/ankurshashcode/docker-slave/blob/alpine/Dockerfile
回答by stacksonstacks
You should keep it simple and use one base image.
Use openjdk7
as base image, install openjdk8 as a package.
This will overwrite openjdk7 as the default JDK while leaving it in the image.
您应该保持简单并使用一个基本图像。
使用openjdk7
作为基本映像,安装openjdk8作为一个包。这将覆盖 openjdk7 作为默认 JDK,同时将其保留在映像中。
# Example Dockerfile
FROM openjdk:7-alpine
RUN apk add --no-cache openjdk8
# Other setup...
Verify
核实
$> java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (IcedTea 3.4.0) (Alpine 8.131.11-r2)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
$> ls /usr/lib/jvm/
default-jvm java-1.7-openjdk java-1.8-openjdk
回答by yamenk
You can use Docker multistage buildto achieve that. You would basically copy the java installation from one image into another image. Here is what the dockerfile might look like:
您可以使用Docker 多阶段构建来实现这一点。您基本上可以将 Java 安装从一个图像复制到另一个图像中。这是 dockerfile 可能的样子:
FROM openjdk:7-alpine as java7
FROM openjdk:8-alpine
COPY --from=java7 /usr/lib/jvm/java-1.7-openjdk /usr/lib/jvm/java-1.7-openjdk
Now you will have both java installations with the jdk7 installation being under /usr/lib/jvm/java-1.7-openjdk
现在您将拥有两个 java 安装和 jdk7 安装 /usr/lib/jvm/java-1.7-openjdk