Docker Community Forums

Share and learn in the Docker community.

Synchronize timezone from host to container

docker

(Marcel Stör) #1

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

(Cadillacs57) #2

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!


(Marcel Stör) #3

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