Docker 使用OpenJDK 验证码无法显示

1.问题复现

在开发时本地使用oraclejdk开发,可正常显示验证码,使用docker打包时,为了降低包大小,切换为openjdk,上线后发现验证码无法正常显示,且后台显示以下异常:

java.lang.NullPointerException: null
	at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
	at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
	at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
	at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
	at sun.font.SunFontManager$2.run(SunFontManager.java:431)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.SunFontManager.(SunFontManager.java:376)
	at sun.awt.FcFontManager.(FcFontManager.java:35)
	at sun.awt.X11FontManager.(X11FontManager.java:57)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
	at java.awt.Font.getFont2D(Font.java:491)
	at java.awt.Font.access$000(Font.java:224)
	at java.awt.Font$FontAccessImpl.getFont2D(Font.java:228)
	at sun.font.FontUtilities.getFont2D(FontUtilities.java:180)
	at sun.font.StandardGlyphVector.initFontData(StandardGlyphVector.java:1126)
	at sun.font.StandardGlyphVector.init(StandardGlyphVector.java:1115)
	at sun.font.StandardGlyphVector.(StandardGlyphVector.java:167)
	at java.awt.Font.createGlyphVector(Font.java:2545)
	at com.google.code.kaptcha.text.impl.DefaultWordRenderer.renderWord(DefaultWordRenderer.java:66)
	at com.google.code.kaptcha.impl.DefaultKaptcha.createImage(DefaultKaptcha.java:43)

2.问题原因:

因为openjdk:8-jre-alpine中缺少FontConfiguration

3.解决方案:

3.1.项目运行在linux中,则需要安装对应的组件

yum install fontconfig

3.2.项目打包在docker中运行

  添加:在docker镜像中安装 ttf-dejavu软件,若验证码中使用了特殊的字体,则需要安装对应字体或使用其他jdk

  更新前Dockerfile文件

#运行此项目基于openjdk镜像
FROM openjdk:8-jre-alpine
#设置容器时间与宿主时间一致
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ADD *.jar app.jar

ENTRYPOINT exec java $JAVA_OPTS   -Djava.security.egd=file:/dev/./urandom  -jar /app.jar

更新后Dockerfile文件

#运行此项目基于openjdk镜像
FROM openjdk:8-jre-alpine
#设置容器时间与宿主时间一致
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk add --update font-adobe-100dpi ttf-dejavu fontconfig
ADD *.jar app.jar

ENTRYPOINT exec java $JAVA_OPTS   -Djava.security.egd=file:/dev/./urandom  -jar /auth-server.jar

3.3 编排文件中安装fontconfig

#运行此项目基于openjdk镜像
FROM openjdk:8-jre-alpine

#安装fontconfig插件
yum install fontconfig
fc-ache --force

#设置容器时间与宿主时间一致
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENTRYPOINT exec java $JAVA_OPTS   -Djava.security.egd=file:/dev/./urandom  -jar /auth-server.jar

 

你可能感兴趣的:(Docker)