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?
-
1Seems to me this has nothing to do with docker but all with your Java program.Henry– Henry2018-07-01 06:28:12 +00:00Commented Jul 1, 2018 at 6:28
-
The program works outside of the Docker container...inside, it outputs "unmappable character for encoding ASCII" when trying to read a French characterJustin Borromeo– Justin Borromeo2018-07-01 06:43:54 +00:00Commented Jul 1, 2018 at 6:43
-
@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.Tom Blodget– Tom Blodget2018-07-01 16:43:54 +00:00Commented Jul 1, 2018 at 16:43
-
@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.Justin Borromeo– Justin Borromeo2018-07-02 17:01:49 +00:00Commented Jul 2, 2018 at 17:01
-
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.Tom Blodget– Tom Blodget2018-07-02 17:08:50 +00:00Commented Jul 2, 2018 at 17:08
Add a comment
|
3 Answers
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
1 Comment
Prabuddha Kulatunga
Instead of set using ENV in Dockerfile, you can use JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 in docker-compose.yml also under environment: option
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
Comments
If Container outputs non-ASCII characters as garbled characters,just add
ENV LANG="C.UTF-8" \
LC_ALL="C.UTF-8"