Be Careful About TimeZone Configuration While Playing With Docker
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:
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.
- You can mount the “/etc/localtime” and “/etc/timezone” files to container from the host machine
Thu Jul 26 00:30:55 EEST 2018# docker run --rm -it \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
Thu Jul 26 00:30:59 EEST 2018
- Add “tzdata” package installation step to your Dockerfile. I’m going to give the example by debian/ubuntu based image:
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.
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