Be Careful About TimeZone Configuration While Playing With Docker

İbrahim Gündüz
Jul 25, 2018 · 2 min read

After spent almost one day, I noticed that docker doesn’t care host machine timezone configuration…

So if you need to do something by current timestamp in your application, you should be careful about timezone configuration of your docker environment because it works independently by the host machine configuration.

Let’s compare date status of host machine and container we’re going to be created:

Call “date” command on the host machine first:

# date
Thu Jul 26 00:10:38 EEST 2018

And.. run a simple docker container in the same machine:

# docker run --rm -it ubuntu:16.04 date
Wed Jul 25 21:11:35 UTC 2018

Ok. Then let’s solve the issue.

Solution 1:

  • You can mount the “/etc/localtime” and “/etc/timezone” files to container from the host machine
# date
Thu Jul 26 00:30:55 EEST 2018
# docker run --rm -it \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
ubuntu:16.04 date
Thu Jul 26 00:30:59 EEST 2018

Solution 2:

  • Add “tzdata” package installation step to your Dockerfile. I’m going to give the example by debian/ubuntu based image:
FROM ubuntu:16.04
RUN apt-get update ;\
apt-get install -y tzdata
  • build the image
# docker build -f Dockerfile -t myimage:latest .

Pass the “TZ” environment variable to container when you created.

# date
Thu Jul 26 00:51:47 EEST 2018
# docker run — rm -e TZ=Europe/Bucharest -it myimage:latest date
Thu Jul 26 00:51:49 EEST 2018

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store