python based Dockerfile throws locale.Error: unsupported locale setting
python based Dockerfile throws locale.Error: unsupported locale setting
我在将主机 (Centos7) 语言环境传递给 python3 docker 图像时遇到问题。尽管我使用了下面 link 中描述的建议,但只有以下语言环境最终出现在图像中:
C
C.UTF-8
POSIX
Why does locale.getpreferredencoding() return 'ANSI_X3.4-1968' instead of 'UTF-8'?
我的 Dockerfile 有:
FROM python:3.7.5
ENV LC_ALL C.UTF-8
WORKDIR /data
ADD ./requirements.txt /data/requirements.txt
RUN pip install -r requirements.txt
COPY . /data
CMD [ "python3", "./test.py" ]
当我运行这个命令时:
locale.setlocale(locale.LC_ALL,'ru_RU')
它抛出这个错误:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
locale.setlocale(locale.LC_ALL,'ru_RU')
File "/usr/local/lib/python3.7/locale.py", line 608, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
如果我设置
ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8
然后我得到:
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
locale.getdefaultlocale ('ru_RU', 'UTF-8')
locale.getpreferredencoding UTF-8
Exception: unsupported locale setting
请解释一下如何将 ru_RU 语言环境添加到 python 图像中?
我会为基于 Debian 的 docker 图片做些什么:
FROM python:3.7.5
RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales
ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8
然后在 python:
import locale
locale.setlocale(locale.LC_ALL,'ru_RU.UTF-8')
对于无法获得接受的答案以使用预期语言环境(例如 en_IN.UTF-8
)的任何人,请注意 /etc/locale.gen
中语言环境中的空格。
试试这个:
FROM python:3.8
RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# en_IN UTF-8/en_IN UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales
ENV LANG en_IN.UTF-8
ENV LC_NUMERIC en_IN.UTF-8
在 dpkg-reconfigure
之后,区域设置应可用 en_IN.UTF-8
我在将主机 (Centos7) 语言环境传递给 python3 docker 图像时遇到问题。尽管我使用了下面 link 中描述的建议,但只有以下语言环境最终出现在图像中:
C
C.UTF-8
POSIX
Why does locale.getpreferredencoding() return 'ANSI_X3.4-1968' instead of 'UTF-8'?
我的 Dockerfile 有:
FROM python:3.7.5
ENV LC_ALL C.UTF-8
WORKDIR /data
ADD ./requirements.txt /data/requirements.txt
RUN pip install -r requirements.txt
COPY . /data
CMD [ "python3", "./test.py" ]
当我运行这个命令时:
locale.setlocale(locale.LC_ALL,'ru_RU')
它抛出这个错误:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
locale.setlocale(locale.LC_ALL,'ru_RU')
File "/usr/local/lib/python3.7/locale.py", line 608, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
如果我设置
ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8
然后我得到:
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
locale.getdefaultlocale ('ru_RU', 'UTF-8')
locale.getpreferredencoding UTF-8
Exception: unsupported locale setting
请解释一下如何将 ru_RU 语言环境添加到 python 图像中?
我会为基于 Debian 的 docker 图片做些什么:
FROM python:3.7.5
RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales
ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8
然后在 python:
import locale
locale.setlocale(locale.LC_ALL,'ru_RU.UTF-8')
对于无法获得接受的答案以使用预期语言环境(例如 en_IN.UTF-8
)的任何人,请注意 /etc/locale.gen
中语言环境中的空格。
试试这个:
FROM python:3.8
RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# en_IN UTF-8/en_IN UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales
ENV LANG en_IN.UTF-8
ENV LC_NUMERIC en_IN.UTF-8
在 dpkg-reconfigure
之后,区域设置应可用 en_IN.UTF-8