基于alpine构建jdk镜像遇到的坑

alpine常用于作为docker的基础镜像,因为它很小,功能精简,基本上没有啥漏洞,记录一下最近用alpine作为基础镜像构建java 8镜像 下面的问题在oracle jdk openjdk都会出现 。

 

错误一

java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11FontManager

  这个错误一般出现在生成验证码绘制的时候,这个错误大概原因就是由于在alpine上太过于精简了,导致初始化FontManagerFactory工厂初始化失败,那么解决办法就是安装glibc
网上有很多博主都只讲安装了glib.apk 核心包就行,其实这里需要安装3个包 以2.3.0为例 需要安装的包为

安装命令如下

apk --no-cache add libstdc++ ca-certificates bash  wget
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-2.30-r0.apk
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-bin-2.30-r0.apk
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-i18n-2.30-r0.apk
apk add glibc-2.30-r0.apk && apk add glibc-bin-2.30-r0.apk && apk add glibc-i18n-2.30-r0.apk

  

安装完后就没有问题了,测试方法 可以写一个main方法在容器中验证是否能执行通过Class.forName("sun.awt.X11FontManager");,当在验证码不可行环境的时候 报错的消息为

/usr/local/jdk1.8.0_301/jre/lib/amd64/libfontmanager.so: libgcc_s.so.1: cann......

还有就是可以进入在运行中的容器直接安装 安装完成了之后基本上就没有问题了。

 

错误二

  该错误在openJDK中出现过,OracleJDK没有出现。

error while loading shared libraries: libz.so.1: cannot open shared object file:

  解决方式就安装zlib 安装命令如下

curl -Ls https://archive.archlinux.org/packages/z/zlib/zlib-1%3A1.2.9-1-x86_64.pkg.tar.xz -o libz.tar.xz && mkdir -p libz && tar -xf libz.tar.xz -C libz

mv libz/usr/lib/libz.so* /usr/glibc-compat/lib

rm -rf libz.tar.xz

  安装好后 没有问题了

 

dragonwell openjdk Dockerfile

另贴上基于alpine的阿里dragonwell openjdk的Dockerfile

dragonwell JDK Dockerfile

 

FROM alpine:3.8
LABEL maintainer="xstao"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && \
    echo ${TZ} > /etc/timezone
#mirrons aliun
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
#install glibc
RUN apk --no-cache add libstdc++ ca-certificates bash  wget curl  && \
    wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub  && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-2.30-r0.apk && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-bin-2.30-r0.apk && \
    wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.30-r0/glibc-i18n-2.30-r0.apk && \
    apk add glibc-2.30-r0.apk && apk add glibc-bin-2.30-r0.apk && apk add glibc-i18n-2.30-r0.apk && \
    curl -Ls https://archive.archlinux.org/packages/z/zlib/zlib-1%3A1.2.9-1-x86_64.pkg.tar.xz -o libz.tar.xz && mkdir -p libz && tar -xf libz.tar.xz -C libz && \
    mv libz/usr/lib/libz.so* /usr/glibc-compat/lib && \
    rm glibc-2.30-r0.apk && rm glibc-bin-2.30-r0.apk && rm glibc-i18n-2.30-r0.apk && rm -rf /var/cache/apk/* && rm -rf libz/* &&  rm -rf libz.tar.xz


RUN apk add --update font-adobe-100dpi ttf-dejavu fontconfig


ENV JAVA_VERSION="jdk8u302-b01"
ENV JAVA_HOME="/usr/local/${JAVA_VERSION}"

ENV PATH="${JAVA_HOME}/bin:${PATH}"
# 
WORKDIR /opt
RUN wget https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.8.9/Alibaba_Dragonwell_8.8.9_x64_linux.tar.gz
RUN tar -zxf Alibaba_Dragonwell_8.8.9_x64_linux.tar.gz
RUN mv ${JAVA_VERSION} /usr/local
RUN rm -rf Alibaba_Dragonwell_8.8.9_x64_linux.tar.gz

  

 

posted @ 2021-11-11 09:48  peachyy  阅读(5823)  评论(0编辑  收藏  举报