11

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?

2 Answers 2

28

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')
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. You meant yum install -y locales since I have Centos. Are you sure if it is possible to call yum in python based image? I have FROM python, but not FROM centos.
It is not a problem. After all, it is still a Linux distro.
I've tested my solution with Debian-based image. Let me know if it works for Centos
From the dockerfile that you posted, it looks like you are using this image hub.docker.com/layers/python/library/python/3.7.5/images/… which seems to be Debian-based. If that's true, my Dockerfile is correct and does not need extra edit
In other words apt-get, not yum :)
|
1

For anyone who can't get the accepted answer to work with intended locales such as en_IN.UTF-8, be wary of spaces in your locale in /etc/locale.gen.

try this:

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

After dpkg-reconfigure, the locale should be available as en_IN.UTF-8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.