LoginSignup
26
19

More than 5 years have passed since last update.

AmazonLinux2のDockerイメージに日本語ローケルを設定してみました。

Last updated at Posted at 2018-04-14

はじめに

AmazonLinux2のDockerイメージに日本語設定をしていみました。
中身の日本語は問題ないと思いますが、ローケル設定後のコマンド系ls, viに表示される
日本語が文字化けするため、調べながら設定してみました。

結論から言うと言語バックであるglibc-langpack-jaをインストールが必要のことです。

前提条件

  • Dockerインストールされていること

 DockerHubからAmazonLinuxのイメージを取得する

取得
$ docker pull amazonlinux:2
確認
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
amazonlinux         2                   ed7831dfe956        4 days ago          208MB

現状の確認

bash
$ docker run -it --rm amazonlinux:2
bash-4.2# touch テスト.txt
bash-4.2# ls
bin  boot  dev  etc  home  lib  lib64  local  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  ?????????.txt

テスト.txtが?????????.txtになっている。

ローケル確認
bash-4.2# locale -a
C
C.utf8
POSIX
en_AG
en_AU
en_AU.utf8
en_BW
en_BW.utf8
en_CA
en_CA.utf8
en_DK
en_DK.utf8
en_GB
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.utf8
en_IE
en_IE.utf8
en_IE@euro
en_IL
en_IN
en_NG
en_NZ
en_NZ.utf8
en_PH
en_PH.utf8
en_SG
en_SG.utf8
en_US
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.utf8
en_ZM
en_ZW
en_ZW.utf8

日本語がないです。

言語パックをインストールする

yumから日本語の言語パックを検索してみる

yum検索
bash-4.2# yum search langpack-ja
Loaded plugins: ovl, priorities
amzn2-core                                                                                                                                                                           | 2.0 kB  00:00:00     
(1/2): amzn2-core/2017.12/x86_64/group_gz                                                                                                                                            | 2.3 kB  00:00:00     
(2/2): amzn2-core/2017.12/x86_64/primary_db                                                                                                                                          |  10 MB  00:00:17     
========================================================================================= N/S matched: langpack-ja =========================================================================================
glibc-langpack-ja.x86_64 : Locale data for ja

  Name and summary matches only, use "search all" for everything.

glibc-langpack-jaでした。早速インストール

インストール
$ yum install -y glibc-langpack-ja
Loaded plugins: ovl, priorities
Resolving Dependencies
--> Running transaction check
---> Package glibc-langpack-ja.x86_64 0:2.26-27.amzn2.0.4 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================
 Package                                              Arch                                      Version                                                 Repository                                     Size
============================================================================================================================================================================================================
Installing:
 glibc-langpack-ja                                    x86_64                                    2.26-27.amzn2.0.4                                       amzn2-core                                    301 k

Transaction Summary
============================================================================================================================================================================================================
Install  1 Package

Total download size: 301 k
Installed size: 2.1 M
Downloading packages:
glibc-langpack-ja-2.26-27.amzn2.0.4.x86_64.rpm                                                                                                                                       | 301 kB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : glibc-langpack-ja-2.26-27.amzn2.0.4.x86_64                                                                                                                                               1/1 
  Verifying  : glibc-langpack-ja-2.26-27.amzn2.0.4.x86_64                                                                                                                                               1/1 

Installed:
  glibc-langpack-ja.x86_64 0:2.26-27.amzn2.0.4                                                                                                                                                              

Complete!

確認と設定

インストールの確認

bash-4.2# locale -a | grep ja
ja_JP.eucjp
ja_JP.utf8

bash-4.2# locale
LANG=
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=

ローケルの設定

ローケル設定
bash-4.2# export LANG='ja_JP.utf8'
bash-4.2# locale
LANG=ja_JP.utf8
LC_CTYPE="ja_JP.utf8"
LC_NUMERIC="ja_JP.utf8"
LC_TIME="ja_JP.utf8"
LC_COLLATE="ja_JP.utf8"
LC_MONETARY="ja_JP.utf8"
LC_MESSAGES="ja_JP.utf8"
LC_PAPER="ja_JP.utf8"
LC_NAME="ja_JP.utf8"
LC_ADDRESS="ja_JP.utf8"
LC_TELEPHONE="ja_JP.utf8"
LC_MEASUREMENT="ja_JP.utf8"
LC_IDENTIFICATION="ja_JP.utf8"
LC_ALL=

確認

確認
bash-4.2# ls
bin  boot  dev  etc  home  lib  lib64  local  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  テスト.txt

日本語が表示される。

まとめてDockerイメージ作成

Dockerfile

Dockerfile
FROM amazonlinux:2

RUN yum install -y glibc-langpack-ja && yum clean all
ENV LANG ja_JP.utf8
ENV LC_ALL ja_JP.utf8

RUN unlink /etc/localtime
RUN ln -s /usr/share/zoneinfo/Japan /etc/localtime

CMD ["/bin/bash"]

イメージをビルドする

ビルドする
$ docker build -t amzn2ja .

確認

確認
$ docker run -it --rm amzn2ja
bash-4.2# ls -al
合計 60
drwxr-xr-x   1 root root 4096  4月 14 22:44 .
drwxr-xr-x   1 root root 4096  4月 14 22:44 ..
-rwxr-xr-x   1 root root    0  4月 14 22:44 .dockerenv
lrwxrwxrwx   1 root root    7  3月 31 08:54 bin -> usr/bin
dr-xr-xr-x   2 root root 4096 10月 20 08:42 boot
drwxr-xr-x   5 root root  360  4月 14 22:44 dev
drwxr-xr-x   1 root root 4096  4月 14 22:44 etc
drwxr-xr-x   2 root root 4096 10月 20 08:42 home
lrwxrwxrwx   1 root root    7  3月 31 08:54 lib -> usr/lib
lrwxrwxrwx   1 root root    9  3月 31 08:54 lib64 -> usr/lib64
drwxr-xr-x   2 root root 4096  3月 31 08:54 local
drwxr-xr-x   2 root root 4096 10月 20 08:42 media
drwxr-xr-x   2 root root 4096 10月 20 08:42 mnt
drwxr-xr-x   2 root root 4096 10月 20 08:42 opt
dr-xr-xr-x 231 root root    0  4月 14 22:44 proc
dr-xr-x---   2 root root 4096 10月 20 08:42 root
drwxr-xr-x   1 root root 4096  4月 14 22:42 run
lrwxrwxrwx   1 root root    8  3月 31 08:54 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 10月 20 08:42 srv
dr-xr-xr-x  13 root root    0  4月 14 16:21 sys
drwxrwxrwt   1 root root 4096  4月 14 22:42 tmp
drwxr-xr-x   1 root root 4096  3月 31 08:54 usr
drwxr-xr-x   1 root root 4096  3月 31 08:54 var
bash-4.2# date
2018年  4月 14日 土曜日 22:44:51 JST

表示OK

以上。

26
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
19