使用 Ubuntu 容器(ascii、utf-8)在 docker(Python、Java、Ruby 等)中运行应用程序时出现编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27931668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:39:08  来源:igfitidea点击:

Encoding Problems when running an app in docker (Python, Java, Ruby, ...) with Ubuntu Containers (ascii, utf-8)

javapythonrubydockerlocale

提问by d0x

On my own PC the application runs nice, but when it gets deployed into docker, it fails because of invalid characters.

在我自己的 PC 上,该应用程序运行良好,但是当它部署到 docker 时,由于字符无效而失败。

I am using the ubuntu:lastestcontainer and python3, javaand ruby.

我正在使用ubuntu:lastest容器 and python3, javaand ruby

回答by d0x

You need to set the localecorrect.

您需要正确设置语言环境

This is the minimal correct Dockerfile:

这是最小的正确 Dockerfile:

FROM ubuntu:latest

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

The usual docker images don't specify a locales. You see it if you bash into it and execute locale:

通常的 docker 图像不指定语言环境。如果你猛击它并执行,你会看到它locale

sudo docker exec -i -t yowsup3 bash

Sources:

资料来源:

回答by Mayank Jain

I tried the above solution and found that the locale-gen command is not available inside my docker.

我尝试了上述解决方案,发现我的 docker 中没有 locale-gen 命令。

so add this line above the RUN command or add it in.

所以在 RUN 命令上方添加这一行或添加它。

RUN apt-get update && apt-get install -y locales && locale-gen en_US.UTF-8

this will now work.

这现在可以工作了。

and second ENV LC_ALL en_US.UTF-8 is enough to set most of the variable but it left with the two vacant so that's why we need all 3 to set.

第二个 ENV LC_ALL en_US.UTF-8 足以设置大部分变量,但它留下了两个空缺,这就是为什么我们需要设置所有 3 个变量。

回答by akhil kailasa

FROM centos:7 

***ENV LANG=en_US.UTF-8***

Adding the above one line in docker file worked for me

在 docker 文件中添加上述一行对我有用