I'm running a Java program from within a Docker container (started with Docker Compose) and it's throwing a bunch of errors caused by UTF-8 characters (as they can't be mapped to the ASCII charset). Is there a way to enable UTF-8 encoding from the docker-compose file?
UTF-8 encoding not working in Docker
Asked Answered
Seems to me this has nothing to do with docker but all with your Java program. –
Aphrodisiac
The program works outside of the Docker container...inside, it outputs "unmappable character for encoding ASCII" when trying to read a French character –
Presbyterian
@JustinBorromeo This proves that you've written your Java program so that it is sensitive to its environment in a way that you don't want it to be. The solution is not to force requirements on the environment but simply to change the program to eliminate its undesired behavior. Please edit to show your code. –
Standstill
@TomBlodget the program is a JUnit test that validates that the code can convert UTF-8 strings to it's simplified ASCII string. There's no way to avoid dealing with UTF-8. –
Presbyterian
The problem is likely when you are using Default rather than UTF-8. There is no such thing as a UTF-8 String or an ASCII String in Java. –
Standstill
You can check by using below command to set java parameters and then try to run your java program -
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
If it worked using above command, set it using an ENV
command during docker image build.
Also if you need to set it in bash_profile, refer below portion of Dockerfile -
RUN echo "JAVA_HOME=/opt/jdk1.8.0_65" >> ~/.bash_profile
Instead of set using ENV in Dockerfile, you can use JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 in docker-compose.yml also under environment: option –
Seyler
Add these lines in Dockerfile
:
RUN echo "LC_ALL=en_US.UTF-8" >> /etc/environment
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
RUN echo "LANG=en_US.UTF-8" > /etc/locale.conf
RUN locale-gen en_US.UTF-8
Source: https://github.com/tianon/docker-brew-debian/issues/45
If Container outputs non-ASCII characters as garbled characters,just add
ENV LANG="C.UTF-8" \
LC_ALL="C.UTF-8"
see https://mcmap.net/q/87482/-how-to-set-the-locale-inside-a-debian-ubuntu-docker-container
© 2022 - 2025 — McMap. All rights reserved.