Docker运行SpringBoot项目报错

一、前因

  SpringBoot应用中有获取图片验证码的接口,使用Docker制作完镜像后,运行镜像,服务日志报如下错误:

java.lang.NullPointerException
    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.<init>(SunFontManager.java:376)
    at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at sun.awt.X11FontManager.<init>(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.java2d.SunGraphics2D.checkFontInfo(SunGraphics2D.java:670)
    at sun.java2d.SunGraphics2D.getFontInfo(SunGraphics2D.java:831)
    at sun.java2d.pipe.GlyphListPipe.drawString(GlyphListPipe.java:50)
    at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2926)
    at com.miaoshaproject.util.CodeUtil.generateCodeAndPic(CodeUtil.java:74)

制作镜像所使用的openjdk的基础版本为:openjdk:8-jdk-alpine,在GitHub上也有人提出过类似的issue:NullPointerException in Alpine JRE 8 Font.

很明显这是由于这个版本的openjdk底层缺少字体库依赖。

二、解决办法

2.1 更换基础镜像,如frolvlad/alpine-oraclejdk8:slim
2.2 在Dockerfile中加入字体库,如:

RUN apk add --no-cache ttf-dejavu

以上两种方式都可以解决此问题~

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐