How to set the locale inside a Debian/Ubuntu Docker container?
Solution 1:
Put in your Dockerfile something adapted from
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
If you run Debian or Ubuntu, you also need to install locales
to have locale-gen
with
apt-get -y install locales
this is extracted from the very good post on that subject, from
http://jaredmarkell.com/docker-and-locales/
Solution 2:
Those who use Debian also have to install locales
package.
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales && \
update-locale LANG=en_US.UTF-8
ENV LANG en_US.UTF-8
This answer helped me a lot.
Solution 3:
Just add
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
into your Dockerfile. (You may need to make sure the locales
package is installed.) Nothing else is needed for the basic operation.
Meanwhile, outside of Ubuntu, locale-gen
doesn’t accept any arguments, that’s why none of the ‘fixes’ using it work e.g. on Debian. Ubuntu have patched locale-gen
to accept a list of locales to generate but the patch at the moment has not been accepted in Debian of anywhere else.
Solution 4:
I actually happened to have suffered from the same problem, but none of the provided answers are 100% working with debian:latest, even if they provide good hints.
The biggest difference is that you should make sure both locales and locales-all are installed, the latter already containing en_US.UTF-8, so you don't have to generate it with local-gen or dpkg-reconfigure.
Here's what I've done in my Dockerfile to make it work:
FROM debian:latest
RUN apt-get update
RUN apt-get install -y locales locales-all
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
Solution 5:
Specify the LANG
and LC_ALL
environment variables using -e
when running your command:
docker run -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 -it --rm <yourimage> <yourcommand>
It's not necessary to modify the Dockerfile.