Setting Locale in Docker
Introduction
Although Python 3 has officially started to use UTF-8 encoding for text files, I still sometimes got errors regarding ASCII/UTF-8 in Docker container. Surprisingly, there is no such issue in the native system. It turns out that it is the system locale problem. In the native system, the locale is usually properly set from the GUI during installation. In Docker container, usually the system locale was not set, and therefore UTF-8 could not be properly read and display in the terminal.
In this blog post, I will talk about how to set the locale properly in Docker container so that there will be no UTF-8 problems at all.
Check System Locale
We could check the system locale using the locale
command.
1 | $ locale |
If the LANG
, LANGUAGE
, and LC_MESSAGES
are not set with UTF-8
locales, you are likely to have UTF-8 read and display issues when running computer programs.
In Python, we could also check the encoding method of the locale in the system using the following command.
1 | $ python -c "import sys; print(sys.stdout.encoding)" |
If the output is not UTF-8
, you are likely to have UTF-8 read and display issues when running computer programs.
It should be noted that the following command, although somewhat similar to the one we used above, does not reflect the system locale.
1 | python -c "import sys; print(sys.getdefaultencoding())" |
Set Locale Properly for Docker Container
It is actually simple to set locale for the Docker container. During the building of Docker image, just add either one of the following Docker script snippets to the Dockerfile and you are all set.
Method 1
1 | RUN apt-get update |
Method 2
1 | RUN apt-get update |
References
Setting Locale in Docker