七、Dockerfile案例一(jdk1.8安装)
七、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到文件夹下。
wget --no-cookies --no-check-certificate \
--header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
[root@centos-docker ~]# mkdir centos-jdk [root@centos-docker ~]# mv jdk-8u141-linux-x64.tar.gz ./centos-jdk/
[root@centos-docker ~]# cd centos-jdk/
[root@centos-docker centos-jdk]# ls
jdk-8u141-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_141 /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-8u141-linux-x64.tar.gz /usr/local/java/
---> 6f93f8ae2f62
Removing intermediate container 763b95ac2d14
Step 5/9 : RUN ln -s /usr/local/java/jdk1.8.0_141 /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 # 出来结果表明配置没问题
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
[root@708a2e94c413 /]# echo $JAVA_HOME
/usr/local/java/jdk
[root@708a2e94c413 /]#
每天一跬步,全年一大步