I'm trying to run a simple Python app in Docker, using the official python:2.7 image.
My problem is that unless I enable -t / --tty in docker run, the python print command terminates when trying to print Unicode characters:
UnicodeEncodeError: 'ascii' codec can't encode characters...
If I enable --tty it works without errors.
The problem is that I want to run this process in the background, using --detach and that mode does not support --tty.
What kind of changes do I need to make to my Dockerfile so that it doesn't terminate when trying to print Unicode strings?
Minimal Dockerfile:
FROM python:2.7
CMD [ "python", "-c", "print (u'\\xc1')" ]
Minimal broken run command:
# error
docker run myimage
# working
docker run --tty myimage
Note: it also works without problems in the official python:3.5 image.
You can run a container with -d and -t.
docker run -dt --name python python:2 python -c 'print (u"\xc1")'
a191fc9b251aa25e078d3537b6bd4a3e1928d39a0cb0104c4a661a9da067e242
$ docker logs python
脕
@yosifkit it wasn't working for me.
Anyway, I found the real fix. It is to set:
ENV PYTHONIOENCODING UTF-8
It might be a good idea to add this to the 2.7 Docker file, but at least anyone can add it to their own Dockerfile. Hopefully this ticket can be found by Google.
Given that we explicitly set ENV LANG C.UTF-8 (http://bugs.python.org/issue19846), I'm +1 on adding ENV PYTHONIOENCODING UTF-8 for Python 2. :+1:
:+1: i always run into that problem and need to google around what environment varible i should add to fix it.
Finally made a PR: https://github.com/docker-library/python/pull/278
Most helpful comment
@yosifkit it wasn't working for me.
Anyway, I found the real fix. It is to set:
It might be a good idea to add this to the 2.7 Docker file, but at least anyone can add it to their own Dockerfile. Hopefully this ticket can be found by Google.