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

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...

Introduction to the process of creating TCP connection in Linux system

Table of contents Steps to create TCP in Linux Se...

A detailed introduction to seata docker high availability deployment

Version 1.4.2 Official Documentation dockerhub st...

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

Table related arrangement and Javascript operation table, tr, td

Table property settings that work well: Copy code ...

How to install jupyter in docker on centos and open ports

Table of contents Install jupyter Docker port map...

Implementation of ssh non-secret communication in linux

What is ssh Administrators can log in remotely to...

MySQL green version setting code and 1067 error details

MySQL green version setting code, and 1067 error ...

Six ways to increase your website speed

1. Replace your .js library file address with the...