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.
/etc/localtime
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.
Best Answer
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.