docker采用Dockerfile安装jdk1.8案例

2022-10-10 00:02
1360
0
1 获取一个简单的Docker系统镜像,并建立一个容器。
  这里我选择下载CentOS镜像
docker pull centos
  通过docker tag命令将下载的CentOS镜像名称换成centos,并删除老标签的镜像文件。
docker tag docker.io/centos centos
docker rmi docker.io/centos
2. JDK的安装和配置
  去Oracle官网提前下载好所需的jdk。
  建立文件夹,并将jdk copy到文件夹下。
[root@centos-docker ~]# mkdir centos-jdk [root@centos-docker ~]# mv jdk-8u144-linux-x64.tar.gz ./centos-jdk/
[root@centos-docker ~]# cd centos-jdk/
[root@centos-docker centos-jdk]# ls
  jdk-8u144-linux-x64.tar.gz
在centos-jdk文件夹中建立Dockerfile,其内容如下:
 
# CentOS with JDK 8
# Author dockerzlnewbie
 
# build a new image with basic centos
FROM centos
# who is the author
MAINTAINER dockerzlnewbie
 
# make a new directory to store the jdk files
RUN mkdir /usr/local/java
 
# copy the jdk archive to the image,and it will automaticlly unzip the tar file
ADD jdk-8u141-linux-x64.tar.gz /usr/local/java/
 
# make a symbol link
RUN ln -s /usr/local/java/jdk1.8.0_144 /usr/local/java/jdk
 
# set environment variables
ENV JAVA_HOME /usr/local/java/jdk
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH ${JAVA_HOME}/bin:$PATH
 
 
构建:注意后边的 . 不能忘了
docker build -t="centos-jdk" .
 
 
查看新建立的镜像
[root@centos-docker centos-jdk]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-jdk latest ad1110b84433 5 minutes ago 503 MB centos latest e8f1bdb3b6a7 2 weeks ago 196.7 MB
建立容器,查看新的镜像中的JDK是否正确
 
[root@centos-docker centos-jdk]# docker run -it centos-jdk /bin/bash
[root@708a2e94c413 /]# java -version  # 出来结果表明配置没问题
//# make a symbol link
RUN ln -s /usr/local/java/jdk1.8.0_144 /usr/local/java/jdk 这个在Dockerfile文件中一定要注意jdk目录路径 我本地多有一个jdk-8u144-linux-x64
  java version "1.8.0_144"
  Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
  Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
  [root@8fd53c588c83 /]# echo $JAVA_HOME
   /usr/local/java/jdk
  [root@8fd53c588c83 /]#
 
启动容器
docker run -di --name=centos-jdk centos-jdk
 
进入容器查看jdk 版本号
docker exec -it centos-jdk /bin/bash
 

全部评论