Docker Container time & timezone (will not reflect changes)

Chockomonkey asked:

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
$ date
Tue Apr 14 23:46:51 UTC 2015

Anyone know what gives?

My answer:

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 anything Linux.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.