How to configure locales to Unicode in a Docker Ubuntu 14.04 container?

49,863

Solution 1

The /etc/default/locale file is loaded by PAM; see /etc/pam.d/login for example. However, PAM is not invoked when running a command in a Docker container. To configure the locale, simply set the relevant environment variable in your Dockerfile. Example:

FROM ubuntu:trusty
ENV LANG en_US.UTF-8
CMD ["/bin/bash"]

Solution 2

I use this in my Dockerfile:

# Set the locale
RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

One can also use the ENV one-liner:

RUN locale-gen en_US.UTF-8
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

Solution 3

Try

ENV LANG C.UTF-8

If you get the unsupported locale setting error and don't want to install any new locales.

Solution 4

I tried this and it helped me in Dockerfile after my image I add

ENV LANG='en_GB.UTF-8' LANGUAGE='en_GB:en' LC_ALL='en_GB.UTF-8'
RUN echo en_GB.UTF-8 UTF-8 >> /etc/locale.gen && locale-gen

and run

docker-compose build
docker-compose up -d

Solution 5

This extended solution solved my issue with python locale in Ubuntu docker container:

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y locales \
    && 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 
ENV LC_ALL en_US.UTF-8
Share:
49,863

Related videos on Youtube

Pierre
Author by

Pierre

Updated on September 18, 2022

Comments

  • Pierre
    Pierre over 1 year

    I installed a Ubuntu Trusty container using Docker, and when I connect to it, I can see that the locale is set to the bare minimum:

    sudo docker run -i -t <id> /bin/bash
    root@<id>:/# locale
    LANG=
    LANGUAGE=
    LC_CTYPE="POSIX"
    LC_NUMERIC="POSIX"
    LC_TIME="POSIX"
    LC_COLLATE="POSIX"
    LC_MONETARY="POSIX"
    LC_MESSAGES="POSIX"
    LC_PAPER="POSIX"
    LC_NAME="POSIX"
    LC_ADDRESS="POSIX"
    LC_TELEPHONE="POSIX"
    LC_MEASUREMENT="POSIX"
    LC_IDENTIFICATION="POSIX"
    LC_ALL=
    

    I need to use an Unicode locale (for instance the US English one, en_US.UTF-8).

    I tried to edit the /etc/default/locale file and put this inside:

    # cat /etc/default/locale 
    LANG=en_US.UTF-8
    

    But even after leaving the container and coming back, the locale is still not properly set.

    I also tried using the update-locale command, without any success:

    root@cab13a6abe4f:/# update-locale LANG=en_US.UTF-8
    root@cab13a6abe4f:/# locale
    LANG=
    LANGUAGE=
    LC_CTYPE="POSIX"
    LC_NUMERIC="POSIX"
    LC_TIME="POSIX"
    LC_COLLATE="POSIX"
    LC_MONETARY="POSIX"
    LC_MESSAGES="POSIX"
    LC_PAPER="POSIX"
    LC_NAME="POSIX"
    LC_ADDRESS="POSIX"
    LC_TELEPHONE="POSIX"
    LC_MEASUREMENT="POSIX"
    LC_IDENTIFICATION="POSIX"
    LC_ALL=
    

    On my local configuration, I have the following configuration:

    LANG=en_US.UTF-8
    LANGUAGE=en_US
    LC_CTYPE="en_US.UTF-8"
    LC_NUMERIC=en_US.UTF-8
    LC_TIME=en_US.UTF-8
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY=en_US.UTF-8
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER=en_US.UTF-8
    LC_NAME=en_US.UTF-8
    LC_ADDRESS=en_US.UTF-8
    LC_TELEPHONE=en_US.UTF-8
    LC_MEASUREMENT=en_US.UTF-8
    LC_IDENTIFICATION=en_US.UTF-8
    LC_ALL=
    

    How can I have the same in my Docker container?

    Thanks in advance!

  • Pierre
    Pierre about 9 years
    It worked! Thanks for the explanation about PAM as well. In the end, I just need to re-build the image (using sudo docker build .) and it works fine.
  • Abe Voelker
    Abe Voelker over 8 years
    This is the correct answer. locale -a shows that the locale has been removed from the base image entirely. locale-gen generates it again and makes it available.
  • Mikhail Batcer
    Mikhail Batcer over 7 years
    Where is this dockerfile? Can it be edited for downloaded image?
  • virushuo
    virushuo over 7 years
    @MikhailBatcer you can create a Dockerfile in your project directory and use the FROM directive to extend an existing image. Then just append the mentioned lines. Find more about the Dockerfile here
  • JustAC0der
    JustAC0der almost 7 years
    I got the following error: locale-gen: not found. Solution: stackoverflow.com/a/39761477/1743367
  • cmoran92
    cmoran92 over 6 years
    You can also avoid rebuilding the image for a single run: docker-compose run --rm -e TERM=xterm -e LANG=C.UTF-8 worker bash