使用Docker构建jdk1.8镜像

时间:2022-12-14 16:38:07

一、下载centos镜像

下载自己需要的版本TAG,详见:

docker安装指定版本TAG的镜像

$ sudo docker pull centos:centos7

二、下载jdk1.8,并上传到/usr/local/src目录,然后解压

$ sudo cd /usr/local/src
$ sudo tar zxf jdk-8u201-linux-x64.tar.gz
$ sudo ls
jdk1.8.0_201  jdk-8u201-linux-x64.tar.gz

三、创建Dockerfile

先在/usr/local目录下创建jdk目录,并将/usr/local/src下的jdk-8u201-linux-x64.tar.gz复制到/usr/local/jdk目录下,然后创建Dockerfile文件

$ sudo mkdir /usr/local/jdk
$ sudo cd /usr/local/jdk
$ sudo cp ../src/jdk-8u201-linux-x64.tar.gz ./
$ sudo ls
jdk-8u201-linux-x64.tar.gz
$ sudo vi Dockerfile
FROM centos:centos7
MAINTAINER tom
RUN mkdir /usr/local/jdk
WORKDIR /usr/local/jdk
ADD jdk-8u201-linux-x64.tar.gz /usr/local/jdk

ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_201
ENV JRE_HOME /usr/local/jdk/jdk1.8.0_201/jre
ENV PATH $JAVA_HOME/bin:$PATH

四、使用Dockerfile构建jdk1.8镜像

$ sudo docker build -t jdk1.8 .
Sending build context to Docker daemon  191.8MB
Step 1/8 : FROM centos:centos7
 ---> 9f38484d220f
Step 2/8 : MAINTAINER tom
 ---> Running in 9ebabac7e9e0
Removing intermediate container 9ebabac7e9e0
 ---> 6e65e30640c0
Step 3/8 : RUN mkdir /usr/local/jdk
 ---> Running in 5805a4f50285
Removing intermediate container 5805a4f50285
 ---> 8ebaedf8bd72
Step 4/8 : WORKDIR /usr/local/jdk
 ---> Running in c68e7f94ce6d
Removing intermediate container c68e7f94ce6d
 ---> f4df19bf9468
Step 5/8 : ADD jdk-8u201-linux-x64.tar.gz /usr/local/jdk
 ---> 1fa5d55acc2e
Step 6/8 : ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_201
 ---> Running in c9c93a299d59
Removing intermediate container c9c93a299d59
 ---> 09b5bfd906dc
Step 7/8 : ENV JRE_HOME /usr/local/jdk/jdk1.8.0_201/jre
 ---> Running in d4509f0c2af8
Removing intermediate container d4509f0c2af8
 ---> 1d6b070b3985
Step 8/8 : ENV PATH $JAVA_HOME/bin:$PATH
 ---> Running in 190dde73be87
Removing intermediate container 190dde73be87
 ---> 50016b7aac0c
Successfully built 50016b7aac0c
Successfully tagged jdk1.8:latest

五、在镜像仓库中查看是否构建成功

$ sudo docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
jdk1.8                    latest              50016b7aac0c        10 seconds ago      598MB

六、启动jdk容器

$ sudo docker run -di --name=jdk1.8 jdk1.8
e04f670691cd301b28fb56c25b12eae4851f583fa7abac367164a0ee68ad7241

七、进入jdk容器,查看是否安装正确(即查看安装之后的目录)

$ sudo docker exec -it jdk1.8 /bin/bash
[root@7d539233feb0 jdk]# pwd
/usr/local/jdk
[root@7d539233feb0 jdk]# ls
jdk1.8.0_201