Solution for Docker container not recognizing fonts such as Songti

Solution for Docker container not recognizing fonts such as Songti

Problem background:

When using docker to deploy the project, the system font is used by default when calling the print control in the project. There is no problem deploying it on Windows. However, when running it in a docker container, the system reports an error and cannot find the corresponding font because there are no related fonts such as Songti in docker.

Solution:

In fact, just like solving the problem that Linux itself does not have Song font, it can also be used to solve the container problem. I found that many docker container problems can actually be solved by following the Linux solution. Basically, you just need to download the Songti ttf and ttc files, then insert them into the container font directory, and restart the container.

Solution:

1. Check the fonts supported by the container.

fc-list :lang=zh

If you see the following situation, it means that font management is not installed and fonts need to be installed

2. Install the font library.

yum -y install fontconfig

After the installation is complete, you can see the fontconfig and fonts folders in the /usr/share directory, as shown in the figure:

3. Copy the downloaded font ttc and ttf files to the /usr/share/fonts file of the container.

cp simsun.ttc container:/usr/share/fonts
cp simsun.ttf container:/usr/share/fonts

4. Then restart the container and you can see the installed Chinese fonts.

5. You can also write this step into the Dockerfile and automatically implement it when building the image.

Supplement: Docker | Install Chinese fonts and change utf-8 encoding in Docker container (solve Chinese garbled characters problem)

1. Project Background

The development team reported that during the project operation, garbled characters appeared when using pagoffice and word to convert to pdf. This problem was solved and this record was made for review.

2. Problem Analysis

There are two problems with garbled characters when using pagoffice and word to convert to pdf, the corresponding relationship is:

pagoffice ---- UTF-8 encoding is not used, and POSIX is used by default in some docker containers.

word2pdf ---- Chinese fonts are not installed in Liunx and are missing. Some Chinese fonts are installed by default in Linux and Docker containers, or there are no Chinese fonts.

To sum up, in order to completely solve the container garbled problem, you need to solve: 1. Use utf-8 to support Chinese; 2. Install Chinese fonts.

3. Solution

3.1. Use UTF-8 encoding to support Chinese

3.1.1 Using C.UTF-8 encoding

Using C.UTF-8 encoding can support Chinese. The Docker image on the official website supports and uses C.UTF-8 encoding by default.

locale -a
#View all language environments of the container
locale
#View the language environment used by the container

If the image you are using is not encoded in C.UTF-8, you can change it to this encoding format by adding the following parameters when running the container:

-e export LANG="C.UTF-8"

3.1.2 Other solutions

You can also add parameters when starting Docker.

-e export LANG="zh_CN.UTF-8"

However, I used the image from the docker official website. After using the parameters, it was indeed set to use "zh_CN.UTF-8", but it was not installed. There will be an error:

locale
# View the set character set
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=zh_CN.UTF-8

LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL= 
locale -a
# View installed character sets
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
C
POSIX
en_US.utf8

3.2. Install Chinese fonts and display Chinese

3.2.1 View existing fonts

View all current fonts in Linux

fc-list

View all current Chinese fonts in Linux

fc-list :lang=zh

3.2.2 Get other fonts

In Windows systems, fonts exist under C:\Windows\Fonts. After copying, upload it to the Linux server /opt/software/chinese (I want to install the font in the docker container. If you want to install the font in the Linux host, put it directly in the server's /usr/share/fonts/ folder).

3.2.2 Installing fonts in containers

docker ps -a
#View container
docker cp /opt/software/chinese tomcat:/usr/share/fonts/
#tomcat is the container name
docker exec -it tomcat /bin/bash
#Enter the container

chmod 644 -R chinese/
Authorization folder
fc-list :lang=zh
#View Chinese fonts 

fc-cache -fv
# Refresh the cache

3.3. Verification

pagoffice ---- After changing the docker container, you need to restart the container

word2pdf ---- refresh the font cache, no need to restart the container

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Use non-root users to execute script operations in docker containers
  • Solution to the Docker container not having permission to write to the host directory
  • Solve the problem of setting Chinese language pack for Docker container
  • Docker win ping fails container avoidance guide
  • How to configure Jupyter notebook in Docker container
  • Docker implements container port binding local port
  • Solve the problem of the container showing Exited (0) after docker run
  • Docker uses root to enter the container

<<:  CSS to achieve glowing text and a little bit of JS special effects

>>:  WEB standard web page structure

Recommend

js implements a simple shopping cart module

This article example shares the specific code of ...

What is the base tag and what does it do?

The <base> tag specifies the default addres...

Seven Principles of a Skilled Designer (2): Color Usage

<br />Previous article: Seven Principles of ...

How to create a web wireframe using Photoshop

This post introduces a set of free Photoshop wire...

A brief discussion on ifnull() function similar to nvl() function in MySQL

IFNULL(expr1,expr2) If expr1 is not NULL, IFNULL(...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

How to install babel using npm in vscode

Preface The previous article introduced the insta...

CocosCreator Universal Framework Design Network

Table of contents Preface Using websocket Constru...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

How to configure VMware multi-node environment

This tutorial uses CentOS 7 64-bit. Allocate 2GB ...

In-depth understanding of the creation and implementation of servlets in tomcat

1. What is a servlet 1.1. Explain in official wor...

A detailed introduction to for/of, for/in in JavaScript

Table of contents In JavaScript , there are sever...