It seems that #13 doesn't fix this problem.
I use the latest images (python 3.5.1).
docker run --rm -it python /bin/bash, and then I execute "locale" in the container, I found the locale is "C.UTF-8".docker run -d python python -m http.server, and then enter into the container and execute "locale", I found it's "POSIX".I don't think it's expected, right?
I am not able to reproduce. :confused: Here is what I tried:
$ docker run -d --name py python:3.5.1 python -m http.server
43b6dc6dd12a1e621b79eace4dc86b0bb28b3c9ea0098917da1add93a44abe07
$ docker exec -it py bash
root@43b6dc6dd12a:/# locale
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_PAPER="C.UTF-8"
LC_NAME="C.UTF-8"
LC_ADDRESS="C.UTF-8"
LC_TELEPHONE="C.UTF-8"
LC_MEASUREMENT="C.UTF-8"
LC_IDENTIFICATION="C.UTF-8"
LC_ALL=
root@43b6dc6dd12a:/#
For method2, I used nsenter to enter into the container. And the locale is "POSIX".
I also tried with docker exec -it py bash, locale is "C.UTF-8".
I found that if you ran bash first, the locale will be "C.UTF-8" but if you ran "python script.py" directly the locale will be "POSIX".
Then I will still get the problem UnicodeEncodeError: 'ascii' codec can't encode character.
I mean it seems adding ENV LANG C.UTF-8 in the Dockerfile doesn't fix the problem.
Now I have to run the container via docker run -e PYTHONIOENCODING=utf-8 to avoid the UnicodeEncodeError problem in #13
Maybe I should drop nsenter and switch to the docker exec -it bash way to enter into the container.
Yeah docker exec replaced nsenter. I still don't know why docker exec to python instead of bash would lose the locale.
I would like to add to this, how to set a foreign locale with the python image?
I am trying this in my Dockerfile
ENV LANG de_DE.UTF-8
ENV LANGUAGE de_DE.UTF-8
ENV LANG_ALL de_DE.UTF-8
ENV LC_ALL de_DE.UTF-8
but get an error message during building
`bash: warning: setlocale: LC_ALL: cannot change locale (de_DE.UTF-8)```
And then the app crashes
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 5: ordinal not in range(128)
How is it possible to set a different locale via Dockerfile?
This is how I solved it for now in my Dockerfile
# Install locales dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
locales \
apt-utils \
&& rm -rf /var/lib/apt/lists/*
# Configure timezone and locale
RUN echo "Europe/Berlin" > /etc/timezone && \
dpkg-reconfigure -f noninteractive tzdata && \
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
sed -i -e 's/# de_DE.UTF-8 UTF-8/de_DE.UTF-8 UTF-8/' /etc/locale.gen && \
echo 'LANG="de_DE.UTF-8"'>/etc/default/locale && \
locale-gen de de_DE de_DE.UTF-8 && \
dpkg-reconfigure --frontend=noninteractive locales && \
update-locale LANG=de_DE.UTF-8
Most helpful comment
This is how I solved it for now in my Dockerfile