Solve the problem of setting Chinese language pack for Docker container

Solve the problem of setting Chinese language pack for Docker container

If you use docker search centos in Docker

Use docker pull docker.io/centos to download the base image

This image does not support Chinese. You can use docker attach container ID to enter the container and use "locale" to view it.

You can use "locale -a" to check the system language package, and you will find that there is no Chinese package

Solution:

1. yum install kde-l10n-Chinese -y install language pack (for centos 7)

2. yum reinstall glibc-common -y to update the gitbc package (because the image has castrated some functions of the package, it needs to be updated)

3. localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 (set system language pack)

4. ENV LC_ALL zh_CN.UTF-8 is set by setting environment variables (you can directly modify the /etc/locale.conf file, but you need to reboot)

5. If you use Dockerfile, you can refer to the following

# Version information FROM docker.io/centos:latest
MAINTAINER mapengfei "[email protected]"
#Set system encoding RUN yum install kde-l10n-Chinese -y
RUN yum install glibc-common -y
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
#RUN export LANG=zh_CN.UTF-8
#RUN echo "export LANG=zh_CN.UTF-8" >> /etc/locale.conf
#ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8

Supplement: Change the character encoding of the Docker container to -zh_CN.UTF-8 Chinese character set

Version History

First edition on April 26, 2019

Problem Description

The company's Java project uses openoffice and pdf2html to convert the file format. Word, ppt, and pdf files are all converted without problems, but excel shows garbled characters after conversion

Background

The project runs in a Docker container, Docker version: Docker version 18.06.1-ce, build e68fc7a

Container system kernel version: 3.10.0-514.el7.x86_64

Container system version: Cent0S7

Cause

After communicating with the architecture, we learned that word, pdf, and ppt files are converted through openoffice and pdf2html, but excel files are not handled in this way. Instead, they are implemented using open source java code on the Internet. After reading the code, the architecture found that this code reads the language configuration of the host system when converting Excel files, so it is determined that the cause of the problem is the system character encoding setting.

Workaround

Checking the system character encoding is fine (note the first three lines), it seems that the character set is set correctly. Check the first three lines for problems. It is said online that this is caused by not installing the Chinese character set.

[root@4273aa460888 opt]# locale
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=

Let's see which character sets are installed on this machine. Damn, there is no Chinese character set installed... (If it is not installed, it will be useless even if you set it up)

[root@4273aa460888 opt]# locale -a
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

Let's take a look at the contents of /etc/locale.conf. Damn, it's still the English character set...

[root@4273aa460888 opt]# cat /etc/locale.conf 
LANG="en_US.UTF-8"

OK, let's install the Chinese character set next

Delete the language configuration in the yum.conf file

sed -i '13'd /etc/yum.conf

Install fonts and glibc

yum install -y kde-l10n-Chinese glibc-common

Let's take a look again. Finally, we have

[root@35b0e9b16498 opt]# locale -a|grep zh
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8
zh_HK
zh_HK.big5hkscs
zh_HK.utf8
zh_SG
zh_SG.gb2312
zh_SG.gbk
zh_SG.utf8
zh_TW
zh_TW.big5
zh_TW.euctw
zh_TW.utf8

Let's modify the /etc/locale.conf file again

echo -e 'export LANG="zh_CN.UTF-8"\nexport LC_ALL="zh_CN.UTF-8"' > /etc/locale.conf
source /etc/locale.conf

Let's take a look at the character encoding again. OK, there are no errors and everything is normal.

[root@35b0e9b16498 opt]# locale
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=zh_CN.UTF-8

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
  • Solution for Docker container not recognizing fonts such as Songti
  • 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

<<:  IE8 compatibility notes I encountered

>>:  Example of how rem is adapted for mobile devices

Recommend

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

How to connect to MySQL remotely through Navicat

Using Navicat directly to connect via IP will rep...

javascript to switch pictures by clicking a button

This article example shares the specific code of ...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...

JavaScript to implement simple tab bar switching content bar

This article shares the specific code of JavaScri...

MySQL 8.0.17 installation and configuration method graphic tutorial

This article shares the installation and configur...

Example operation MySQL short link

How to set up a MySQL short link 1. Check the mys...

Detailed process record of Vue2 initiating requests using Axios

Table of contents Preface Axios installation and ...

Sample code for changing the color of a png image through a CSS3 filter

This method uses the drop-shadow filter in CSS3 t...

A simple LED digital clock implementation method in CSS3

This should be something that many people have do...

About the location of the H1 tag in XHTML

There has been a lot of discussion about H1 recent...

How to set a fixed IP address in CentOS7 virtual machine

Since my development environment is to install Ce...

The iframe frame sets the white background to transparent in IE browser

Recently, I need to frequently use iframe to draw ...