Fixing Missing Locale Setting in Ubuntu Docker Image

Today at talpasolutions we happened to bounce into a strange error when dockerizing a qt5 based data processing application which errored on startup because of missing locale settings.

In contrast to an interactively installed Ubuntu where you choose (and set) your language settings during the installation process these appear to not be set in a Docker image.

You can easily test this by:

docker run --rm ubuntu:18.04 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=67c1fcdca15d
HOME=/root

docker run --rm ubuntu:18.04 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=

# same seems to apply for Debian Docker images

Although the error message by the qt5 application

Caught unhandled exception. what():
locale::facet::_S_create_c_locale name not valid

is pretty self-explaining it turned out that it was not as easy to fix this as we might have expected ... actually it also took quite a while to really narrow it down to the above mentioned missing locale settings.

So after a lot of "googling" and quite a few attempts to set locales (non-interactively) in a Dockerfile this post (where else than on Stack Overflow) revealed the (in this case) working solution:

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

The thread also lists a few other approaches as well ... for us the above worked and after having set this and configuring the rest of the applications Dockerfile settings the application started and worked as expected.