Docker 和 Java - FontConfiguration 问题

2022-09-02 12:55:28

我们有一个Java应用程序,它使用第三方生成Word文档(Asposee,但我认为这在这里并不重要)。该应用程序是从一个简单的 Docker 文件构建的:

FROM openjdk:10-jdk-slim
COPY target/*.jar /opt/
CMD $JAVA_HOME/bin/java $JAVA_OPTS -jar /opt/*.jar

当我们在本地构建应用程序(然后)并在其中运行应用程序时,它可以很好地工作。mvn packagedocker buildk8s

但是,当我们使用 Jenkins 在 CI/CD 管道中构建映像时,在运行显然需要其他字体的特定进程时,我们会遇到运行时异常:

Caused by: java.lang.NullPointerException: null
    at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1288)
    at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
    at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107)
    at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:765)
    at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:440)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:385)
    at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:56)

在这种情况下,该项目在 Jenkins 中是 buit 的,由 docker 映像 编译。maven:3.5.4-jdk-10-slim

我已经检查了两个jar文件(本地和来自jenkins),并且类文件是相同的(如预期的那样)。

在这两种情况下,它都是相同的基本图像,所以我不明白可能有什么区别。在本地构建时,Docker 中是否有其他 Docker 容器与在另一个 Docker 容器中构建时有何不同?

编辑

我们研究了这两个 Docker 映像,发现了以下差异。

由于本地构建的映像返回:ls -l /usr/lib

drwxr-xr-x  2 root root  4096 May  3  2017 X11
drwxr-xr-x  5 root root  4096 Apr 26 00:00 apt
drwxr-xr-x  2 root root  4096 May 26 08:31 binfmt.d
drwxr-xr-x  2 root root  4096 Jun  6 01:50 cgmanager
drwxr-xr-x  2 root root  4096 Jun  6 01:50 dbus-1.0
drwxr-xr-x  2 root root  4096 Jun  6 01:51 dconf
drwxr-xr-x  3 root root  4096 Jun  6 01:51 debug
drwxr-xr-x  3 root root  4096 Apr 20 10:08 dpkg
drwxr-xr-x  2 root root  4096 Jun  6 01:50 environment.d
drwxr-xr-x  3 root root  4096 Apr 25 04:56 gcc
drwxr-xr-x  2 root root  4096 Jun  6 01:51 glib-networking
drwxr-xr-x  2 root root  4096 Apr 26 00:00 init
drwxr-xr-x  1 root root  4096 Jun  6 01:51 jvm
drwxr-xr-x  3 root root  4096 Jun  6 01:50 kernel
lrwxrwxrwx  1 root root    20 Mar  4 09:49 libnih-dbus.so.1 -> libnih-dbus.so.1.0.0
-rw-r--r--  1 root root 34824 Mar  4 09:49 libnih-dbus.so.1.0.0
lrwxrwxrwx  1 root root    15 Mar  4 09:49 libnih.so.1 -> libnih.so.1.0.0
-rw-r--r--  1 root root 92184 Mar  4 09:49 libnih.so.1.0.0
drwxr-xr-x  3 root root  4096 Mar 29 19:47 locale
drwxr-xr-x  3 root root  4096 Jun  6 01:50 lsb
drwxr-xr-x  1 root root  4096 Jul 21  2017 mime
drwxr-xr-x  2 root root  4096 Jun  6 01:50 modprobe.d
drwxr-xr-x  2 root root  4096 May 26 08:31 modules-load.d
-rw-r--r--  1 root root   198 Jan 13 23:36 os-release
drwxr-xr-x  3 root root  4096 Jun  6 01:51 ssl
drwxr-xr-x  1 root root  4096 Jun  6 01:50 systemd
drwxr-xr-x  2 root root  4096 Jun  6 01:50 sysusers.d
drwxr-xr-x  2 root root  4096 Jul 21  2017 tar
drwxr-xr-x 15 root root  4096 Feb 11 20:06 terminfo
drwxr-xr-x  1 root root  4096 Jun  6 01:50 tmpfiles.d
drwxr-xr-x  1 root root  4096 Apr 26 00:00 udev
drwxr-xr-x  1 root root 16384 Jun  6 01:51 x86_64-linux-gnu

但是在 Jenkins 构建的图像返回内部:ls -l /usr/lib

drwxr-xr-x  5 root root 4096 Jun 25 00:00 apt
drwxr-xr-x  3 root root 4096 Jul  3 01:00 debug
drwxr-xr-x  3 root root 4096 Apr 20 10:08 dpkg
drwxr-xr-x  3 root root 4096 Jun 17 03:36 gcc
drwxr-xr-x  2 root root 4096 Jun 25 00:00 init
drwxr-xr-x  1 root root 4096 Jul  3 01:00 jvm
drwxr-xr-x  1 root root 4096 Jul 12 11:00 locale
drwxr-xr-x  3 root root 4096 Jul  3 01:00 lsb
drwxr-xr-x  1 root root 4096 May 16 07:47 mime
-rw-r--r--  1 root root  198 Jan 13 23:36 os-release
drwxr-xr-x  3 root root 4096 Jul  3 01:00 ssl
drwxr-xr-x  3 root root 4096 Apr 20 10:08 systemd
drwxr-xr-x  2 root root 4096 May 16 07:47 tar
drwxr-xr-x 15 root root 4096 May 21 08:54 terminfo
drwxr-xr-x  2 root root 4096 Jun 25 00:00 tmpfiles.d
drwxr-xr-x  3 root root 4096 Jun 25 00:00 udev
drwxr-xr-x  2 root root 4096 May  3  2017 X11
drwxr-xr-x  1 root root 4096 Jul  3 01:00 x86_64-linux-gnu

这真的很令人费解,因为我认为Docker总是会从相同的Dockerfile中生成相同的图像。


答案 1

使用 openjdk:8u111-jdk-alpine,安装 dejavu 可以解决问题:

例如:

Dockerfile:

FROM openjdk:8u111-jdk-alpine
# Needed to fix 'Fontconfig warning: ignoring C.UTF-8: not a valid language tag'
ENV LANG en_GB.UTF-8

# JRE fails to load fonts if there are no standard fonts in the image; DejaVu is a good choice,
# see https://github.com/docker-library/openjdk/issues/73#issuecomment-207816707
RUN apk add --update ttf-dejavu && rm -rf /var/cache/apk/*

VOLUME /tmp
COPY /target/*.jar app.jar
ENTRYPOINT ["java","-Xmx100m","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

答案 2

安装为我解决了问题(来源):libfontconfig1

RUN apt-get install -y libfontconfig1 && rm -rf /var/lib/apt/lists/*

推荐