docker采用Dockerfile安装jdk1.8案例

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
复制代码

根据Dokcerfile创建新镜像:

复制代码
# 注意后边的 . 不能忘了
[root@centos-docker centos-jdk]# docker build -t="centos-jdk" .
Sending build context to Docker daemon 563.3 MB
Step 1/9 : FROM centos
 ---> 49f7960eb7e4
Step 2/9 : MAINTAINER dockerzlnewbie
 ---> Running in 236e645d059e
 ---> 46f1ec0e9185
Removing intermediate container 236e645d059e
Step 3/9 : RUN mkdir /usr/local/java
 ---> Running in 5bcabd89ff57

 ---> 850644b108f9
Removing intermediate container 5bcabd89ff57
Step 4/9 : ADD jdk-8u144-linux-x64.tar.gz /usr/local/java/
 ---> 6f93f8ae2f62
Removing intermediate container 763b95ac2d14
Step 5/9 : RUN ln -s /usr/local/java/jdk1.8.0_144 /usr/local/java/jdk
 ---> Running in 3fa23091d220

 ---> e65ff28dcd74
Removing intermediate container 3fa23091d220
Step 6/9 : ENV JAVA_HOME /usr/local/java/jdk
 ---> Running in 3377346d4bf3
 ---> e7ded6e2877b
Removing intermediate container 3377346d4bf3
Step 7/9 : ENV JRE_HOME ${JAVA_HOME}/jre
 ---> Running in e91d136905cf
 ---> 673c511de63e
Removing intermediate container e91d136905cf
Step 8/9 : ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
 ---> Running in 4b26957ba236
 ---> e0bc33b6a9e0
Removing intermediate container 4b26957ba236
Step 9/9 : ENV PATH ${JAVA_HOME}/bin:$PATH
 ---> Running in a70c96627f21
 ---> a2bc40b96e4a
Removing intermediate container a70c96627f21
Successfully built a2bc40b96e4a
[root@centos-docker 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 /]#

复制代码
posted @ 2019-07-05 17:37  Angelasp  阅读(10149)  评论(0编辑  收藏  举报
by angelasp.com