I've created a docker container, which is running in the wrong timezone. I personally need it to run in my timezone (Europe/Stockholm, GMT+1). Since the project is open source, other users may also wish to change that. I wish to make it easy for anyone to change this in e.g. the Dockerfile or in the docker-compose.yml.

If possible, I also wish that the solution is applicable not only on CentOS in case someone wishes to use a different distro.

What is the most distro-agnostic approach to set the localtime/timezone in my docker container?

These two approaches seem to be common but I'm not sure if they are really the best way forward for me:

TZ environment variable

Some Linux distros read the TZ environment variable. However, I'm noticing it doesn't work when I use the centos:7 image.


You can map the container's /etc/localtime to /etc/localtime on the host in the docker-compose.yml. But when doing this, distros that use /etc/timezone is left at UTC and software which may be reading that will then read the wrong timezone.

  • /etc/localtime is the ultimate fallback location. I don’t understand your last paragraph on it, though. It’s a symlink in most cases. Copying the actual file to the container should produce the desired result. – Daniel B May 16 '16 at 11:32
  • @DanielB I meant that even if you take care of /etc/localtime it seems some software rely on /etc/timezone. So therefore I was wondering if the /etc/localtime approach was really the best one. – fredrik May 16 '16 at 11:35
  • I guess that software needs to be fixed, then. /etc/localtime is available virtually everywhere, including OS X and FreeBSD. – Daniel B May 16 '16 at 11:43
  • 1
    Some distros like: RUN echo "Europe/Stockholm" > /etc/timezone Others like: RUN ln -s /usr/share/zoneinfo/Europe/Stockholm /etc/localtime – joat Sep 9 '16 at 2:19

Distro-agnostic would require a script that detects which distro is being used. This is because each distro "family" has a different way of noting timezones. For some, adding the following to the Dockerfile might work:

RUN echo "Europe/Stockholm" > /etc/timezone

Other (e.g., Debian) require using the above and then following with another Dockerfile command like:

RUN RUN sudo dpkg-reconfigure -f noninteractive tzdata

Other distros (IIRC, including CentOS) would require something like:

RUN ln -s /usr/share/zoneinfo/Europe/Stockhom /etc/localtime

Making it distro-agnostic would require researching each distro's method for re-configuring the timezone. If you look at /etc/localtime, you'll notice that it's not a text file.

  • And some applications ignore the OS timezone setting anyway :-7. e.g. jenkins is java based, you need to pass some java arguments to set timezone in the jenkins service/start script. So Distro-agnostic and App-agnostic would have to cover more scenarios than just /etc/localtime and /etc/timezone – gaoithe Jan 22 '19 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.