How to set the locale inside a Ubuntu Docker conta

2019-01-05 01:10发布

I'm running a Ubuntu Docker container. I have a Norwegian keyboard and need to use Norwegian characters (øæå).

My Terminal character encoding is set to UTF-8 and I'm connected to my container using SSH. However, I'm unable to type Norwegian characters, nor copy and paste Norwegian characters, nor use CTL+SHIFT+U+00f8.

I tried:

locale-gen nb_NO.UTF-8

but nothing changed. How do I set the locale and keyboard inside a Docker container?

标签: ubuntu docker
6条回答
疯言疯语
2楼-- · 2019-01-05 01:15

Those who use Debian also have to install locales package.

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8

ENV LANG en_US.UTF-8 

This answer helped me a lot.

查看更多
虎瘦雄心在
3楼-- · 2019-01-05 01:18

Put in your Dockerfile something adapted from

# Set the locale
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8     

this is extracted from the very good post on that subject, from

http://jaredmarkell.com/docker-and-locales/

查看更多
Summer. ? 凉城
4楼-- · 2019-01-05 01:26

Just add

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

into your Dockerfile. Nothing else is needed for the basic operation. Meanwhile, locale-gen doesn’t accept any arguments, that’s why none of the ‘fixes’ using it work.

查看更多
狗以群分
5楼-- · 2019-01-05 01:27

I dislike having Docker environment variables when I do not expect user of a Docker image to change them. I think the following is much cleaner:

echo "LC_ALL=en_US.UTF-8" >> /etc/environment

(Edit: This does not work. It seems I tested it badly initially.)

Just put it somewhere in one RUN. If you do not have UTF-8 locales generated, then you can do the following set of commands:

export DEBIAN_FRONTEND=noninteractive
apt-get update -q -q
apt-get install --yes locales
locale-gen --no-purge en_US.UTF-8
update-locale LANG=en_US.UTF-8
echo locales locales/locales_to_be_generated multiselect en_US.UTF-8 UTF-8 | debconf-set-selections
echo locales locales/default_environment_locale select en_US.UTF-8 | debconf-set-selections
dpkg-reconfigure locales
查看更多
时光不老,我们不散
6楼-- · 2019-01-05 01:34

I actually happened to have suffered from the same problem, but none of the provided answers are 100% working with debian:latest, even if they provide good hints.

The biggest difference is that you should make sure both locales and locales-all are installed, the latter already containing en_US.UTF-8, so you don't have to generate it with local-gen or dpkg-reconfigure.

Here's what I've done in my Dockerfile to make it work:

FROM debian:latest
RUN apt-get update
RUN apt-get install -y locales locales-all
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8
查看更多
混吃等死
7楼-- · 2019-01-05 01:39

Specify these environment variables when running your command:

docker run -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 -it --rm <yourimage> <yourcommand>

It's not necessary to modify the Dockerfile.

查看更多
登录 后发表回答