5

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?

5
  • 1
    Seems to me this has nothing to do with docker but all with your Java program. Commented 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 character Commented 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. Commented 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. Commented 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. Commented Jul 2, 2018 at 17:08

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of set using ENV in Dockerfile, you can use JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 in docker-compose.yml also under environment: option
4

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

0

If Container outputs non-ASCII characters as garbled characters,just add

ENV LANG="C.UTF-8" \
    LC_ALL="C.UTF-8"

see https://stackoverflow.com/a/41648500/20813300

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.