Skip to content

Python 2.7 inside Docker cannot print unicode without --tty #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hyperknot opened this issue Sep 17, 2016 · 5 comments
Closed

Python 2.7 inside Docker cannot print unicode without --tty #147

hyperknot opened this issue Sep 17, 2016 · 5 comments
Labels
Issue Request Request for image modification or feature

Comments

@hyperknot
Copy link

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.

@hyperknot hyperknot changed the title Python inside Docker cannot print unicode without --tty Python 2.7 inside Docker cannot print unicode without --tty Sep 17, 2016
@yosifkit
Copy link
Member

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
Á

@hyperknot
Copy link
Author

@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.

@tianon
Copy link
Member

tianon commented Sep 19, 2016

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. 👍

@xsteadfastx
Copy link

👍 i always run into that problem and need to google around what environment varible i should add to fix it.

@wglambert wglambert added Request Request for image modification or feature Issue labels Apr 24, 2018
@tianon
Copy link
Member

tianon commented Apr 25, 2018

Finally made a PR: #278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue Request Request for image modification or feature
Projects
None yet
Development

No branches or pull requests

5 participants