Synchronize timezone from host to container

I’m surprised there seems to be no easy and cross-platform way to synchronize the timezone from host to container at runtime. So, the timezone in the container should not be set when it’s built but when it’s started. So far I found the following two options:

a) docker run -v /etc/timezone:/etc/timezone:ro
b) docker run -e "TZ=Asia/Kolkata"

Both are a no-go for my use case. a) works only on Linux as there’s no /etc/timezone on Windows and macOS. b) requires the user to manually set the desired timezone (i.e. the same as on the host).

Are these really all sensible options?

For the sake of completeness doing this at build time in the Dockerfile you’d use something like this I learned:

RUN sudo echo "America/New_York" > /etc/timezone
RUN sudo dpkg-reconfigure -f noninteractive tzdata
2 Likes

Hey man!, thanks for your post, i was confused about the TZ thing and getting an extreme difference in date, 2017 date time and when using TZ 2018 date! it was not working as expected.
now it works fine and i can keep coding my Django project

Thanks again for your tip!

I’m glad you found this useful. There are two things I since learned.

  1. For the docker run -e TZ={timezone} workaround to work tzdata of course has to be installed in the container you’re trying to run.
  2. What I was initially looking for (Docker container automatically having same timezone as host system) can be achieved through an ugly hack in the run script. It can query geoip.ubuntu.com (or any other geo IP database) once it is started on the new network and then set the server’s timezone based on the response: https://askubuntu.com/a/565139

I’m surprised that there is no reliable way to pass the host’s timezone to the container.

I am running the containers with Kubernetes so for me setting the environment variable dynamically is not an option, and since I don’t know in advance whether the host will have /etc/timezone or not I can not rely on the volume mount solution either.

Weird :confused:

Hi there,

thanks for this post! On Windows I created a file named timezone and added it to the volume list like this:
docker run -v c:/<your_path>/timezone:/etc/timezone:ro ...

Even if this may not be Your preferred way, You would have to change only one entry to change timezones for Your docker images within windows.

On MacOS you can get the timezone from /etc/localtime. I use this to set the TZ env variable:

export TZ=$(readlink /etc/localtime | sed 's#/var/db/timezone/zoneinfo/##');

Then you can do this:

docker run -e "TZ=${TZ}"

Hey Matthias,
What is in that file that you created? I would like to set the timezone in all my containers to be the same as my windows host. Would you like to share that file you created with us who are trying to solve this timezone issue?
Thanks in advance.
S

Hi S,

The file just contains a timezone according https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. In my case Europe/Zurich

Regards
Matthias

timezone.txt (13 Bytes)

P.S. The extension .txt was required to get the file uploaded here.

Hello Matthias,
Thanks for the info.

I am running a yml file from an open source.
In the yml file, beside the timezone, the script also has a line for localtime in the volume session. Currently it is:

/etc/localtime:/etc/localtime:ro

For timezone, the info is a constant base on where one is located, as shown in your timezone file.
But for localtime, it is a variable. From your experience, how would you syn the localtime in the windows host with the localtime inside the container? aka how to substitute “/etc/localtime” usage in windows environment?
Thanks in advance.
Sorcerer

Hello Sorcerer,

Honestly, I have not dealt with it since I’ve switched to Linux as a docker base by now, because I had several other problems with docker and windows. I was using docker for windows only on one machine. On that Windows machine, I simply installed linux with docker in a vm using Hyper-V.

Hope You find a suitable solution for You.

Regards
Matthias