一 工作目录
root@k8s-harbor-01:/data/dockerfile/web/jdk# pwd
/data/dockerfile/web/jdk
二 下载jdk
下载地址:https://www.oracle.com/java/technologies/downloads/#java8
三 编写Dockerfile
root@k8s-harbor-01:/data/dockerfile/web/jdk# cat Dockerfile
FROM 192.168.174.120/baseimages/alpine_glibc-base:3.15
ADD jdk-8u311-linux-x64.tar.gz /usr/local/
RUN ln -sv /usr/local/jdk1.8.0_311/ /usr/local/jdk \
&& echo "export JAVA_HOME=/usr/local/jdk" >> /etc/profile \
&& echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH" >> /etc/profile \
&& echo "export CLASSPATH=.\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin
四 编写构建镜像脚本
root@k8s-harbor-01:/data/dockerfile/web/jdk# cat build-command.sh
#!/bin/bash
docker build -t 192.168.174.120/baseimages/jdk-base:1.8.0_311 .
#docker push 192.168.174.120/baseimages/jdk-base:1.8.0_311
五 构建镜像
root@k8s-harbor-01:/data/dockerfile/web/jdk# ./build-command.sh
Sending build context to Docker daemon 888.7MB
Step 1/7 : FROM 192.168.174.120/baseimages/alpine_glibc-base:3.15
---> 6a740bc2390f
Step 2/7 : ADD jdk-8u311-linux-x64.tar.gz /usr/local/
---> 57ad5640fcd2
Step 3/7 : RUN ln -sv /usr/local/jdk1.8.0_311/ /usr/local/jdk && echo "export JAVA_HOME=/usr/local/jdk" >> /etc/profile && echo "export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH" >> /etc/profile && echo "export CLASSPATH=.\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar" >> /etc/profile
---> Running in 21a566764594
'/usr/local/jdk' -> '/usr/local/jdk1.8.0_311/'
Removing intermediate container 21a566764594
---> 86f1884b8456
Step 4/7 : ENV JAVA_HOME /usr/local/jdk
---> Running in 1ed761acc179
Removing intermediate container 1ed761acc179
---> db335fa9cf53
Step 5/7 : ENV JRE_HOME $JAVA_HOME/jre
---> Running in 8fcf140062e7
Removing intermediate container 8fcf140062e7
---> 10fd183602ca
Step 6/7 : ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
---> Running in 07867634f80d
Removing intermediate container 07867634f80d
---> 5edea6817d5a
Step 7/7 : ENV PATH $PATH:$JAVA_HOME/bin
---> Running in 7f9421d00f96
Removing intermediate container 7f9421d00f96
---> f7f2cc224b22
Successfully built f7f2cc224b22
Successfully tagged 192.168.174.120/baseimages/jdk-base:1.8.0_311
六 验证镜像
root@k8s-harbor-01:/data/dockerfile/web/jdk# docker run -it --rm 192.168.174.120/baseimages/jdk-base:1.8.0_311 java -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
七 上传镜像到harbor
root@k8s-harbor-01:/data/dockerfile/web/jdk# docker push 192.168.174.120/baseimages/jdk-base:1.8.0_311