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?
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: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:
Usually it is sufficient to set an environment variable in the docker container, like so:
Of course this would work also with
docker-compose
.You can add your local files (/etc/timezone and /etc/localtime) as volume in your docker-container.
Update your
docker-compose.yml
with the following lines.Now the container time is the same as on your host
Mounting
/etc/localtime
in the image, so it is in sync withhost -v
is the most popular one.But see issue 12084:
In ubuntu 16.04 image there is bug. Solution was
Adding my two cents here, because I've tried several of these but none worked on alpine-based images.
However, this did the trick:
[Source]
if you are using docker image based on
ubuntu
:In alpine basic Image (example use node:10.16.0-alpine):
Using a Fedora container (likely to work with ubuntu also):
The simplest solution I found was to use the following in docker-compose.yml
Then in your .env file (which docker-compose automatically reads)
This allows you to put docker-compose.yml under version control and use a customized .env file which can be ignored by git.
You get a default value for the container and you get customization, best of both worlds.
For Fedora no other changes were necessary, it just works!
Thanks to VonC for the information and link to the issue. This seems like such a convoluted mess, so I did some testing on my own idea of how to solve this and it seems to work great.
(follow prompts to select my timezone)
Then I updated my Dockerfiles to reflect this:
There must be something wrong with this because it seems too easy to be overlooked... Or is this acceptable?