1. 准备中文字体
将所有需要添加的中文字体放进一个文件夹内,例如 fonts
2. 创建 Dockerfile 文件
Dockefile 文件与 fonts 文件夹放在同一目录下
# 使用 slim 版本的 jdk 17 镜像作为基础镜像
FROM openjdk:17-slim# 安装字体工具
RUN apt-get update && \apt-get install -y fontconfig && \rm -rf /var/lib/apt/lists/*# 创建存放字体的目录
RUN mkdir -p /usr/share/fonts/chinese# 将本地的字体文件复制到镜像中
COPY ./fonts /usr/share/fonts/chinese/# 更新字体缓存
RUN fc-cache -fv
3. 构建镜像
docker build -t openjdk:17-jre-slim-chinese .
构建过程日志
[+] Building 61.3s (10/10) FINISHED=> [internal] load .dockerignore 0.1s=> => transferring context: 2B 0.1s=> [internal] load build definition from Dockerfile 0.1s=> => transferring dockerfile: 444B 0.1s=> [internal] load metadata for docker.io/library/openjdk:17-slim 3.4s=> [1/5] FROM docker.io/library/openjdk:17-slim@sha256:aaa3b3cb27e3e520b8f116863d0580c438ed55ecfa0bc126b41f68c3f62f9774 28.4s=> => resolve docker.io/library/openjdk:17-slim@sha256:aaa3b3cb27e3e520b8f116863d0580c438ed55ecfa0bc126b41f68c3f62f9774 0.0s=> => sha256:aaa3b3cb27e3e520b8f116863d0580c438ed55ecfa0bc126b41f68c3f62f9774 547B / 547B 0.0s=> => sha256:779635c0c3d23cc8dbab2d8c1ee4cf2a9202e198dfc8f4c0b279824d9b8e0f22 953B / 953B 0.0s=> => sha256:37cb44321d0423bc57266a3bff658daf00478e4cdf2d3b8091f785310534256d 4.80kB / 4.80kB 0.0s=> => sha256:1fe172e4850f03bb45d41a20174112bc119fbfec42a650edbbd8491aee32e3c3 31.38MB / 31.38MB 10.0s=> => sha256:44d3aa8d076675d49d85180b0ced9daef210fe4fdff4bdbb422b9cf384e591d0 1.58MB / 1.58MB 3.6s=> => sha256:6ce99fdf16e86bd02f6ad66a0e1334878528b5a4b5487850a76e0c08a7a27d56 187.90MB / 187.90MB 23.8s=> => extracting sha256:1fe172e4850f03bb45d41a20174112bc119fbfec42a650edbbd8491aee32e3c3 4.5s=> => extracting sha256:44d3aa8d076675d49d85180b0ced9daef210fe4fdff4bdbb422b9cf384e591d0 0.3s=> => extracting sha256:6ce99fdf16e86bd02f6ad66a0e1334878528b5a4b5487850a76e0c08a7a27d56 4.4s=> [internal] load build context 10.6s=> => transferring context: 337.13MB 10.6s=> [2/5] RUN apt-get update && apt-get install -y fontconfig && rm -rf /var/lib/apt/lists/* 20.7s=> [3/5] RUN mkdir -p /usr/share/fonts/chinese 0.4s=> [4/5] COPY ./fonts /usr/share/fonts/chinese/ 3.4s=> [5/5] RUN fc-cache -fv 2.6s=> exporting to image 2.1s=> => exporting layers 2.1s=> => writing image sha256:6d19d72c7994a66498f3eec248fed6a09f35efaf6b2d1507c71e8e2d44b257e8 0.0s=> => naming to openjdk:17-jre-slim-chinese
4. 确认字体安装
- 以新镜像启动容器
docker run -it --rm openjdk:17-jre-slim-chinese bash
- 进入容器内部,查看字体
root@2755c79aaf21:/# fc-list :lang=zh /usr/share/fonts/chinese/FZKTJW_0.TTF: FZKai\-Z03S,方正楷体简体:style=Regular /usr/share/fonts/chinese/SIMYOU.TTF: YouYuan,幼圆:style=Regular /usr/share/fonts/chinese/msyhbd.ttc: Microsoft YaHei UI:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia /usr/share/fonts/chinese/FZXBSJW_0.TTF: FZXiaoBiaoSong\-B05S,方正小标宋简体:style=Regular /usr/share/fonts/chinese/FZSTK.TTF: FZShuTi,方正舒体:style=Regular /usr/share/fonts/chinese/Deng.ttf: DengXian,等线:style=Regular /usr/share/fonts/chinese/FZHTJW_0.TTF: FZHei\-B01S,方正黑体简体:style=Regular /usr/share/fonts/chinese/simsun.ttc: SimSun,宋体:style=Regular,常规