基于python的Dockerfile抛出语言环境错误:不支持的语言环境设置 [英] python based Dockerfile throws locale.Error: unsupported locale setting

查看:206
本文介绍了基于python的Dockerfile抛出语言环境错误:不支持的语言环境设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将主机(Centos7)的语言环境传递到python3 docker映像时遇到问题.即使我使用了下面链接中描述的建议,图像中也只有以下区域设置:

I have a problem with passing the host's (Centos7) locales to the python3 docker image. Only the following locales end up in the image, even though I used the suggestion described in the link below:

C
C.UTF-8
POSIX

为什么locale.getpreferredencoding()返回'ANSI_X3.4-1968'而不是'UTF-8'?

我的Dockerfile具有:

My Dockerfile has:

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图像中?

Please, explain how can I add a ru_RU locale into the python image?

推荐答案

对于基于Debian的docker映像我该怎么做:

What I would do for Debian based docker image:

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')

这篇关于基于python的Dockerfile抛出语言环境错误:不支持的语言环境设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆