镜像瘦身

JDK瘦身

1. 解压缩

  1. # 解压(下载JRE见“参考”)
  2. tar -zxvf jre-8u221-linux-x64.tar.gz
  3. # 查看jre大小(瘦身前大概229M,瘦身后大概111M)
  4. du -sh jre1.8.0_221

2. 瘦身

  1. ############################ JRE瘦身开始 ############################
  2. cd jre1.8.0_221
  3. rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txtTHIRDPARTYLICENSEREADME.txt Welcome.html \
  4. rm -rf lib/plugin.jar \
  5. lib/ext/jfxrt.jar \
  6. bin/javaws \
  7. lib/javaws.jar \
  8. lib/desktop \
  9. plugin \
  10. lib/deploy* \
  11. lib/*javafx* \
  12. lib/*jfx* \
  13. lib/amd64/libdecora_sse.so \
  14. lib/amd64/libprism_*.so \
  15. lib/amd64/libfxplugins.so \
  16. lib/amd64/libglass.so \
  17. lib/amd64/libgstreamer-lite.so \
  18. lib/amd64/libjavafx*.so \
  19. lib/amd64/libjfx*.so
  20. ############################ JRE瘦身结束 ############################

3. 重新打包

  1. cd ../
  2. tar -zcvf jre-8u221-linux-x64-pure.tar.gz jre1.8.0_221

4. 编辑Dockerfile

  1. vi jdk.Dockerfile

内容如下:

  1. FROM alpine:latest
  2. MAINTAINER polaris "450733605@qq.com"
  3. WORKDIR /docker/jdk
  4. ADD jre-8u221-linux-x64-pure.tar.gz /docker/jdk/
  5. # glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
  6. COPY glibc-2.29-r0.apk /docker/jdk/
  7. COPY glibc-bin-2.29-r0.apk /docker/jdk/
  8. COPY glibc-i18n-2.29-r0.apk /docker/jdk/
  9. # 更新Alpine的软件源为阿里云
  10. RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
  11. echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
  12. RUN apk update && apk upgrade
  13. # apk类似于ubuntu的apt-get
  14. RUN apk --no-cache add ca-certificates wget \
  15. && wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
  16. && apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
  17. && rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk
  18. # 配置环境变量
  19. ENV JAVA_HOME=/docker/jdk/jre1.8.0_221
  20. ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  21. ENV PATH=$JAVA_HOME/bin:$PATH
  22. # 容器启动时需要执行的命令
  23. #CMD ["java","-version"]

5. 构建镜像

  1. # 构建的镜像大概166MB
  2. docker build -f jdk.Dockerfile -t jdk8:v1.0.0 .

6. 创建并启动容器

  1. # 方式1
  2. docker create -it --name jdk8_pure jdk8:v1.0.0
  3. docker start jdk8_pure
  4. # 方式2
  5. docker run -itd --name jdk8_pure jdk8:v1.0.0
  6. docker ps |grep jdk8_pure
  7. docker start jdk8_pure
  8. docker restart jdk8_pure
  9. docker stop jdk8_pure
  10. docker rm jdk8_pure

7. 测试JDK

  1. docker exec -it jdk8_pure java -version

8. 备份

  1. # 导出镜像(159MB)
  2. docker save -o docker-jdk-1.8.221-pure.save.tar jdk8:v1.0.0
  3. docker save > docker-jdk-1.8.221-pure.save.tar jdk8:v1.0.0
  4. # 加载镜像
  5. docker load -i docker-jdk-1.8.221-pure.save.tar
  6. docker load < docker-jdk-1.8.221-pure.save.tar
  7. # 运行容器
  8. 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