1 獲取一個簡單的Docker系統鏡像,並創建一個容器。java
這裏我選擇下載CentOS鏡像linux
docker pull centos
經過docker tag命令將下載的CentOS鏡像名稱換成centos,並刪除老標籤的鏡像文件。docker
docker tag docker.io/centos centos
docker rmi docker.io/centos
2. JDK的安裝和配置centos
去Oracle官網提早下載好所需的jdk。bash
創建文件夾,並將jdk copy到文件夾下。ui
[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,其內容以下:spa
# 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建立新鏡像:code
# 注意後邊的 . 不能忘了
[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]#
查看新創建的鏡像blog
[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是否正確ip
[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 /]#