Answer a question

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.

Answers

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:

environment=LANG="es_ES.utf8", LC_ALL="es_ES.UTF-8", LC_LANG="es_ES.UTF-8"

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):

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 into the right direction.

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐