I am trying to run my python file that first reads a string in Chinese language and print 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
I then run :
docker build -t python-example .
docker run python-example
Error i got: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)
When i run it locally, it works fine.
Short version
Put this in your
Dockerfile
:or as mentioned in the comments above pass it on the command line:
Long version:
When you start the Python interpreter, Python has to set up
stdout
in order to send output to your terminal. On your modern O/S, your terminal probably reports that it supports UTF-8 or some other advanced encoding. You can see what encoding is used by running this command:When you run a docker container, the environment variables Python would expect to use a more advanced encoding are not present, and so Python will fall back to a basic character set to ensure compatibility. You can verify this by running the same command in your container:
When we pass
PYTHONIOENCODING
we see thesys.stdout.encoding
is set appropriately:Read about
PYTHONIOENCODING
in the Python documentation. This answer also goes into great detail about encoding/decoding andstdout
.I add the below command in my docker file:
then build/rebuild docker images, you'd better add this in the base image.
I ran into the same issue while I was deploying a Django application with supervisor and gunicorn.
What fixed it was to add the following line to my supervisor config file:
For your case make sure that the chinese locale that you want to print is available and installed in your docker container. This blog describes how to do it: example dockerfile (use the chinese locale instead of en_CA.UTF-8):
hopefully this leads you into the right direction.