Troubleshooting | Characters not rendering properly with Docker containers

Troubleshooting

Recently I have to investigate an issue where it works fine locally with both Linux and Windows local dev setup and failing in Docker-based container deployment. The issue was some of the characters like £ are printing as ?? symbol.

Since the issue works fine locally, the initial guess was it might be something related to Docker deployment.

Let’s troubleshoot this kind of issue.

Locale command- you can get the information of locale-specific information using the below command

locale

in my case, I run the above command in VM. output was like below

In general, C is for computer. you can refer to meaning for each LANG, LANGUAGE, LC_CTYPE, etc here.

Normally we could set our LANG to en_US or en_GB or your preferred language rather than using system one like above.

locale -a

if we execute the above command you can find all possible locales available.

C
C.UTF-8
en_US.UTF-8
POSIX

if the desired locale is not available you can generate it using “locale-gen” command and set the locale accordingly. [ not going into details on how to do it. Internet has a lot of resources on this]

Even locale is set to a known locale in VM, our issue still not resolved.

Then log into the docker container and run the above commands again. ( in my case I am running an application on docker swarm with multiple containers)

Here POSIX means basically another name for C locale. which means container locale is not set to proper locale, hence this kind of issue we are troubleshooting can occur.

Let’s fix this.

How to fix this? Basically, we need to add the locale-gen program to your container, generate and set the locale to a known locale like en_US.

In order to do the above changes, we need to change the existing docker image. you can add the below snippet into your Dockerfile

RUN apt-get clean && apt-get update
RUN apt-get install locales
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

you can change the locale as your wish and only set the required types of LC types. Again if you run locale command, you can see that all required locales are set to the locale you set in the image.

Finally, with this change, I was able to resolve the character printing issue I faced.

Thanks for reading! (A few 👏 are always appreciated. 😀)

Senior Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store