为任何用户在 Dockerfile 中的 debian stretch 中设置语言环境

Kar*_*son 2 debian locale docker

RUN apt-get update && \
    # Install locales
    apt-get install -y locales && \
    # Set locale
    sed --in-place '/en_US.UTF-8/s/^# //' /etc/locale.gen && \
    locale-gen && \
    # Set system locale (add line)
    echo "export LANG=en_US.UTF-8" >> /etc/profile && \
    # Set system timezone (add line)
    echo "export TZ=UTC" >> /etc/profile && \
    # If the user will not change, we must source
    source /etc/profile
Run Code Online (Sandbox Code Playgroud)

我在 GitLab 管道中使用该图像,并显示以下内容:

$ cat /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi
export LANG=en_US.UTF-8
export TZ=UTC
$ locale
LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
Run Code Online (Sandbox Code Playgroud)

如果有人可以解释语言环境在 debian 中的实际工作原理。我已经阅读了仅适用于 Ubuntu 的其他答案:https : //stackoverflow.com/questions/28405902/how-to-set-the-locale-inside-a-docker-container http://jaredmarkell.com/docker-and -地区/

小智 5

在那里解释:http : //jaredmarkell.com/docker-and-locales/

根据此站点和您的链接,区域设置由这些指令定义到 Dockerfile 中:

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  
Run Code Online (Sandbox Code Playgroud)

Ubuntu 仅在 debian 衍生版上,并且 docker 集成与两个发行版非常相似。

还要注意将 sed 放入第一个文件中,您只需取消注释(删除空格不是强制性的)

sed --in-place '/en_US.UTF-8/s/^#//'
Run Code Online (Sandbox Code Playgroud)