我正在基於
Ubuntu 14.04
構建docker映像
並且無法正確設置語言環境。
(我知道14.04是EOL,但它仍然可以正常執行,對吗?我被Askubuntu.com上的Ubuntu使用者拒绝了支援,因此我在這裏很幸運)
我的
Dockerfile
看起来像:
COPY .bashrc /root/.bashrc
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get install -y build-essential \
locales
RUN dpkg-reconfigure locales
WORKDIR /home/prs/
哪裏
.bashrc
是:
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
它將正確構建
docker build -t "test" .
但是当我執行它時,得到以下輸出:
$ docker run -it test
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
[email protected]:/# source /root/.bashrc
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
[email protected]:/#
我也尝試過:
echo "LANG=en_US.UTF-8" > /etc/locale.conf
我仍然得到:
# locale-gen
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
這是怎麼迴事? 以及如何正確設置語言環境?
UPDATE:
如果我將容器映像基於
Ubuntu 18.04
,則好像
,我要選擇一个
locale
当我键入
# dpkg-reconfigure locales
但是用
14.04
(這是我当前需要的),我得到以下資訊:
# dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_US.UTF-8",
LC_ALL = "en_US.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
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
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
根据EduardoTrápani(@EduardoTrápani)的建議,我還尝試手動執行區域設置:
# echo "en_US.UTF-8 UTF-8" | sudo tee -a /etc/locale.gen
en_US.UTF-8 UTF-8
# locale-gen
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
另一个提示可能是,locales / sup [ported.d目錄中没有任何內容(這應该是
locale-gen
的結果)
如果我从http://manpages.ubuntu.com/manpages/precise/man8/locale-gen.8.html正確理解了這一點:
# ls -l /var/lib/locales/supported.d/
总計0
- 5月前1 #
- 5月前2 #
您看到的問题是,預設情况下,除
C
之外,Docker容器中未內建任何語言環境 (也稱為POSIX
)和C.UTF-8
(UTF-8版本).這是因為glibc附帶了大量的語言環境,這些語言環境占用了相当大的空間,而Debian和Ubuntu為了节省空間,仅在locales
中發佈了壓縮版本 軟體包,要求在系統上構建特定的語言環境。如果您只是想使用UTF-8查詢英語語言環境,請
C.UTF-8
可能会满足您的需求,並且避免在容器中構建新的語言環境.您可以簡單地使用LC_ALL=C.UTF-8 docker run -it test
但是,如果您特別需要美国英語語言環境(例如,因為您出於某種奇怪的原因而確實類似Avoirdupois系統,並且希望將其与
LC_MEASUREMENT
一起使用, ),最簡單的解決方案是安裝locales-all
包.提供所有語言環境的預構建版本。或者,如果您迫切需要空間,則可以更新Dockerfile以添加以下行:
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen
這將只構建您想要的語言環境。
最终的解決方案是让您的
.bashrc
檢測您的語言環境是否有效,然後將其設置為其他語言.由於尽管我会說英語,但我经常使用法語語言環境,但並非在所有使用的系統上都可用,因此我采用了這種方法.您可以執行以下命令,如果語言環境為$locale
,則將退出0 存在,如果不存在則為非零$ perl -MPOSIX=locale_h -E 'exit !setlocale(LC_ALL, $ARGV[0]);' "$locale" \ 2>/dev/null
相似問題
- 在Ubuntu 2004中没有sudo的Docker?ubuntudockerubuntu20.042021-01-03 11:56
您應將語言環境添加到
/etc/locale.gen
,構建它,然後就可以使用它:您可以將這些命令放入docker檔案中。