为什么在 docker 中更改 ubuntu16.04 时区无效

why change ubuntu16.04 timezone in docker is invalid

我使用命令apt-get install -y tzdata && echo Asia/Shanghai > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata将时区更改为UTC+8,但它不起作用。我的Dockerfile如下:

FROM ubuntu:xenial
MAINTAINER Leon.Gan
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial main" >/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial main" >>/etc/apt/sources.list \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main" >>/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main" >>/etc/apt/sources.list \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial universe" >>/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe" >>/etc/apt/sources.list \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe" >>/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe" >>/etc/apt/sources.list \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-security main" >>/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main" >>/etc/apt/sources.list \
  && echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe" >>/etc/apt/sources.list \
  && echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe" >>/etc/apt/sources.list \
  && apt-get update \
  && apt-get install -y locales \
  && locale-gen zh_CN.UTF-8 \
  && apt-get install -y tzdata \
  && echo Asia/Shanghai > /etc/timezone \
  && dpkg-reconfigure -f noninteractive tzdata \
  && rm -rf /var/lib/apt/lists/*

ENV LC_ALL "zh_CN.UTF-8"
ENV LANG "zh_CN.UTF-8"

CMD ["/bin/bash"]

构建镜像后,进入容器,运行命令cat /etc/timezone,内容还是Etc/UTC。这意味着我在 Dockerfile 中的命令 echo Asia/Shanghai > /etc/timezone 不起作用。所以,请教我如何正确更改时区,谢谢。

尝试使用

ENV TZ=Asia/Singapore

在你的 dockerfile 中。正在将全局环境时区设置为正确的时区。

我假设 dpkg-reconfigure -f noninteractive tzdata 做错了什么。

我建议将 ENV TZ=Asia/Shanghai and/or 设置为 运行

ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime