Docker Python install utf-8 locale
I am trying to run my python file which reads a Chinese string first and prints it.
This is my Dockerfile
FROM python:2.7-onbuild
ENV LANG en_US.UTF-8
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
This is my python file:
# -*- coding: utf-8 -*-
import jieba
s = "ζζ₯ε°εδΊ¬ζΈ
ε倧ε¦"
s = s.decode('utf-8')
print type(s), s
Then I run:
docker build -t python-example .
docker run python-example
The error I received was: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)
When I run it locally, it works fine.
source to share
I faced the same problem when I deployed a Django app with supervisor and machine gun.
Fixed: add the following line to the dispatcher config file:
environment=LANG="es_ES.utf8", LC_ALL="es_ES.UTF-8", LC_LANG="es_ES.UTF-8"
In your case, make sure the Chinese language you want to print is available and installed in the docker container. This blog describes how to do it: example dockerfile (use Chinese instead of en_CA.UTF-8):
FROM ubuntu:15.10
MAINTAINER Mobify <ops@mobify.com>
RUN apt-get -qq update && \
apt-get -q -y upgrade && \
apt-get install -y sudo curl wget locales && \
rm -rf /var/lib/apt/lists/*
# Ensure that we always use UTF-8 and with Canadian English locale
RUN locale-gen en_CA.UTF-8
COPY ./default_locale /etc/default/locale
RUN chmod 0755 /etc/default/locale
ENV LC_ALL=en_CA.UTF-8
ENV LANG=en_CA.UTF-8
ENV LANGUAGE=en_CA.UTF-8
hopefully this leads you in the right direction.
source to share
Short version
Put this in your Dockerfile
:
ENV PYTHONIOENCODING=utf-8
or, as pointed out in the comments above, pass it on the command line:
docker run -e PYTHONIOENCODING=utf-8 my-python-image some-command
Long version:
When you start the Python interpreter, Python must be configured stdout
to send output to your terminal. On your modern O / S, your terminal is probably reporting that it supports UTF-8 or some other extended encoding. You can find out what encoding is used when running this command:
$ python -c 'import sys; print(sys.stdout.encoding)'
UTF-8
When you start a docker container, Python environment variables will expect to use a more advanced encoding, and therefore Python will fall back to base charset to ensure compatibility. You can check this by running the same command in your container:
$ docker run my-python-image python -c 'import sys; print(sys.stdout.encoding)'
ANSI_X3.4-1968
When we go through PYTHONIOENCODING
, we see what is sys.stdout.encoding
set accordingly:
$ docker run -e PYTHONIOENCODING=UTF-8 my-python-image python -c 'import sys; print(sys.stdout.encoding)'
UTF-8
Read about PYTHONIOENCODING
in the Python documentation . This answer also provides details on encoding / decoding andstdout
.
source to share