问题原文
原帖链接 · Chockomonkey · CC BY-SA 3.0
Where do Docker containers get their time information? I've created some containers from the basic ubuntu:trusty image, and when I run it and request 'date', I get UTC time.
For awhile I got around this by doing the following in my Dockerfile:
RUN sudo echo "America/Los_Angeles" > /etc/timezone
However, for some reason that stopped working. Searching online I saw the below suggested:
docker run -v /etc/timezone:/etc/timezone [image-name]
Both these methods correctly set the timezone though!
$ cat /etc/timezone
America/Los_Angeles
$ date
Tue Apr 14 23:46:51 UTC 2015
Anyone know what gives?
已采纳解答
原帖链接 · Michael Hampton · CC BY-SA 4.0
The secret here is that dpkg-reconfigure tzdata simply creates /etc/localtime as a copy, hardlink or symlink (a symlink is preferred) to a file in /usr/share/zoneinfo. So it is possible to do this entirely from your Dockerfile. Consider:
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
And as a bonus, TZ will be set correctly in the container as well.
This is also distribution-agnostic, so it works with pretty much any Linux.
Note: if you are using an alpine based image you have to install the tzdata first. (see this issue here)
Looks like this:
RUN apk add --no-cache tzdata
ENV TZ America/Los_Angeles
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
