Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python based Dockerfile throws locale.Error: unsupported locale setting

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

Why does locale.getpreferredencoding() return 'ANSI_X3.4-1968' instead of 'UTF-8'?

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" ]

When I run this command:

locale.setlocale(locale.LC_ALL,'ru_RU')

it throws this error:

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

If I set

ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8

Then I get:

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

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

like image 272
khassen Avatar asked Jan 07 '20 17:01

khassen


1 Answers

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

and then in python:

import locale

locale.setlocale(locale.LC_ALL,'ru_RU.UTF-8')
like image 180
kendriu Avatar answered Nov 06 '22 04:11

kendriu