docker debian使用中文

docker输入中文乱码

问题

docker输入中文乱码

原因

默认没有加载zh_CN.UTF-8编码,默认是POSIX

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
root@dbf1a20c8d34:/data# locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
注意到没有locales相关文件,所以需要安装locales软件
1
2
3
sudo apt-get install locales -y
No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76
Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module)
装的时候又报错
1
sudo apt install libterm-readkey-perl dialog -y
需要注意这里的文件/etc/locale.gen,这里面是系统支持的编码,这里我们使用zh_CN.UTF8,反注释那一行
1
2
sed -ie 's/^# zh_CN.UTF-8/zh_CN.UTF-8/' /etc/locale.gen
locale-gen

此时我们执行locale还是POSIX

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
root@dbf1a20c8d34:/data# locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
环境变量设置LANG为中文
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
sudo apt-get install vim -y
root@dbf1a20c8d34:/data# locale
LANG=zh_CN.UTF-8
LANGUAGE=
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=
root@dbf1a20c8d34:/data# grep LANG /etc/profile
export LANG="zh_CN.UTF-8"

这样就可以在文件输入中文了