9

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?

khassen
  • 93
  • 1
  • 6

2 Answers2

21

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')
kendriu
  • 565
  • 3
  • 21
  • 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. – khassen Jan 08 '20 at 13:40
  • It is not a problem. After all, it is still a Linux distro. – kendriu Jan 08 '20 at 15:08
  • I've tested my solution with Debian-based image. Let me know if it works for Centos – kendriu Jan 08 '20 at 15:10
  • From the dockerfile that you posted, it looks like you are using this image https://hub.docker.com/layers/python/library/python/3.7.5/images/sha256-75ac684e1b4ec87174779576fa6181034e3a069ef879361fbaea04a74959a5f1 which seems to be Debian-based. If that's true, my Dockerfile is correct and does not need extra edit – kendriu Jan 08 '20 at 15:16
  • In other words `apt-get`, not `yum` :) – kendriu Jan 08 '20 at 15:17
  • Thank you! This solved the problem. Now locales -a shows these ones in the image: C C.UTF-8 POSIX ru_RU.utf8 – khassen Jan 13 '20 at 13:40
  • Great! Thanks for approving the anwser – kendriu Jan 14 '20 at 18:17
  • Worked for ubuntu 20.04 docker image and de_DE locale. – Wu Wei Jan 20 '21 at 13:20
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

seanick
  • 83
  • 1
  • 8