docker构建jdk镜像并创建容器

1.创建目录

mkdir –p /usr/local/dockerjdk8

2.下载jdk-8u171-linux-x64.tar.gz并上传到服务器(虚拟机)中的/usr/local/dockerjdk8目录
3.创建文件Dockerfile并上传到服务器(虚拟机)中的/usr/local/dockerjdk8目录

#依赖镜像名称和ID
FROM centos:7
#指定镜像创建者信息
MAINTAINER ITCAST
#切换工作目录
WORKDIR /usr
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把java添加到容器中
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH

4.执行命令构建镜像

[root@192 dockerjdk8]# docker build -t='jdk1.8' .
Sending build context to Docker daemon  190.9MB
Step 1/9 : FROM centos:7
7: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:7
 ---> 9f38484d220f
Step 2/9 : MAINTAINER ITCAST
 ---> Running in ad1494178ff2
Removing intermediate container ad1494178ff2
 ---> 4aa8a5d51063
Step 3/9 : WORKDIR /usr
 ---> Running in 0f66282a61c9
Removing intermediate container 0f66282a61c9
 ---> 506282c918ce
Step 4/9 : RUN mkdir /usr/local/java
 ---> Running in 4e9485cfd355
Removing intermediate container 4e9485cfd355
 ---> 92bf6c018a65
Step 5/9 : ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
 ---> 1870df96c70d
Step 6/9 : ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
 ---> Running in 6c374eba712d
Removing intermediate container 6c374eba712d
 ---> a84828d2eebf
Step 7/9 : ENV JRE_HOME $JAVA_HOME/jre
 ---> Running in 2451f3aa2bcb
Removing intermediate container 2451f3aa2bcb
 ---> 56ade40daf8e
Step 8/9 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
 ---> Running in 07b81d6d5756
Removing intermediate container 07b81d6d5756
 ---> 6ba5fb28742b
Step 9/9 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Running in 7fd221177706
Removing intermediate container 7fd221177706
 ---> b0c56b203b56
Successfully built b0c56b203b56
Successfully tagged jdk1.8:latest

5.查看镜像是否建立完成

docker images

6.创建容器

[root@192 ~]# docker run -di --name=jdk jdk1.8
866408971d7473bd04d45da88ed89d9104590aa87c2417e0959a4ee831ad4

你可能感兴趣的:(docker构建jdk镜像并创建容器)