镜像瘦身
JDK瘦身
1. 解压缩
# 解压(下载JRE见“参考”)
tar -zxvf jre-8u221-linux-x64.tar.gz
# 查看jre大小(瘦身前大概229M,瘦身后大概111M)
du -sh jre1.8.0_221
2. 瘦身
############################ JRE瘦身开始 ############################
cd jre1.8.0_221
rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txtTHIRDPARTYLICENSEREADME.txt Welcome.html \
rm -rf lib/plugin.jar \
lib/ext/jfxrt.jar \
bin/javaws \
lib/javaws.jar \
lib/desktop \
plugin \
lib/deploy* \
lib/*javafx* \
lib/*jfx* \
lib/amd64/libdecora_sse.so \
lib/amd64/libprism_*.so \
lib/amd64/libfxplugins.so \
lib/amd64/libglass.so \
lib/amd64/libgstreamer-lite.so \
lib/amd64/libjavafx*.so \
lib/amd64/libjfx*.so
############################ JRE瘦身结束 ############################
3. 重新打包
cd ../
tar -zcvf jre-8u221-linux-x64-pure.tar.gz jre1.8.0_221
4. 编辑Dockerfile
vi jdk.Dockerfile
内容如下:
FROM alpine:latest
MAINTAINER polaris "450733605@qq.com"
WORKDIR /docker/jdk
ADD jre-8u221-linux-x64-pure.tar.gz /docker/jdk/
# glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
COPY glibc-2.29-r0.apk /docker/jdk/
COPY glibc-bin-2.29-r0.apk /docker/jdk/
COPY glibc-i18n-2.29-r0.apk /docker/jdk/
# 更新Alpine的软件源为阿里云
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade
# apk类似于ubuntu的apt-get
RUN apk --no-cache add ca-certificates wget \
&& wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
&& rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk
# 配置环境变量
ENV JAVA_HOME=/docker/jdk/jre1.8.0_221
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
# 容器启动时需要执行的命令
#CMD ["java","-version"]
5. 构建镜像
# 构建的镜像大概166MB
docker build -f jdk.Dockerfile -t jdk8:v1.0.0 .
6. 创建并启动容器
# 方式1
docker create -it --name jdk8_pure jdk8:v1.0.0
docker start jdk8_pure
# 方式2
docker run -itd --name jdk8_pure jdk8:v1.0.0
docker ps |grep jdk8_pure
docker start jdk8_pure
docker restart jdk8_pure
docker stop jdk8_pure
docker rm jdk8_pure
7. 测试JDK
docker exec -it jdk8_pure java -version
8. 备份
# 导出镜像(159MB)
docker save -o docker-jdk-1.8.221-pure.save.tar jdk8:v1.0.0
docker save > docker-jdk-1.8.221-pure.save.tar jdk8:v1.0.0
# 加载镜像
docker load -i docker-jdk-1.8.221-pure.save.tar
docker load < docker-jdk-1.8.221-pure.save.tar
# 运行容器
docker run -itd --name jdk8_pure jdk8:v1.0.0
参考
CSDN:Docker Alpine制作jdk、瘦身版jre
https://blog.csdn.net/qq_43059674/article/details/103356158
Oracle:Java SE 8 Archive Downloads (JDK 8u211 and later)
https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html
GitHub:glibc lib Downloads
https://github.com/sgerrand/alpine-pkg-glibc/releases