1、yum命令安裝jdk
選擇版本安裝
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
或者如下命令,安裝jdk1.8.0的所有文件
yum install -y java-1.8.0-openjdk*
yum install -y java-1.8.0-openjdk.x86_64
2、查看JDK是否安裝成功
# java -version
[root@1c164311bb8b /]# java -version openjdk version "1.8.0_222" OpenJDK Runtime Environment (build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
3、配置環境變量
JDK默認安裝路徑/usr/lib/jvm
[root@1c164311bb8b jvm]# pwd /usr/lib/jvm [root@1c164311bb8b jvm]# [root@1c164311bb8b jvm]# ll total 0 drwxr-xr-x. 3 root root 17 Jul 30 05:48 java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64 lrwxrwxrwx. 1 root root 21 Jul 30 05:48 jre -> /etc/alternatives/jre lrwxrwxrwx. 1 root root 27 Jul 30 05:48 jre-1.8.0 -> /etc/alternatives/jre_1.8.0 lrwxrwxrwx. 1 root root 35 Jul 30 05:48 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk lrwxrwxrwx. 1 root root 51 Jul 30 05:48 jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64 -> java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre lrwxrwxrwx. 1 root root 29 Jul 30 05:48 jre-openjdk -> /etc/alternatives/jre_openjdk [root@1c164311bb8b jvm]#
在/etc/profile文件添加如下命令
# set java environment JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64 PATH=$PATH:$JAVA_HOME/bin CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME CLASSPATH PATH
保存關閉profile文件,執行如下命令生效
source /etc/profile
4、編寫Dockerfile文件時,只需要定義容器變量即可(第三部可以省略)
ENV JAVA_HOME /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64
參考博客:
DOCKER 安裝CENTOS,TOMCAT,JDK等相關的自定義(DOCKERFILE)鏡像
CentOS 7 yum 安裝與配置 JDK
https://blog.csdn.net/github_38336924/article/details/82221258