Why mount /etc/timezone and /etc/localtime in containers?
from talkingpumpkin@lemmy.world to selfhosted@lemmy.world on 08 Dec 00:07
https://lemmy.world/post/22889332

A lot of selfhosted containers instructions contain volume mounts like:

docker run ...
  -v /etc/timezone:/etc/timezone:ro \
  -v /etc/localtime:/etc/localtime:ro \
  ...

but all the times I tried to skip those mounts everything seemed to work perfectly.

Are those mounts only necessary in specific cases?

PS:

Bonus question: other containers instructions say to define the TZ variable. Is that only needed when one wants a container to use a different timezone than the host?

#selfhosted

threaded - newest

ptz@dubvee.org on 08 Dec 00:25 next collapse

Most containers default to UTC, and depending what you’re running, that may be fine.

I only mount /etc/timezone / /etc/localtime if I’m running a container where it needs to be on the same timezone as the host (DB containers, anything where I want the logs in local time, etc). Not all containers use the TZ env var, so bind mounting the timezone files from the host is a guaranteed way to sync them.

DaGeek247@fedia.io on 08 Dec 01:20 next collapse

It's so the container has the correct local time. It doesn't matter unless you're trying to schedule things in the container and don't want to calculate the offset every time you do.

mbirth@lemmy.ml on 08 Dec 01:31 next collapse

If the container instructions say to set the TZ variable, this means that they bring all the necessary timezone information (for all timezones around the world) with them inside the container. For Alpine Linux, this would be the 1.5 MB (uncompressed) tzdata package.

If you are instructed to link to those files on the host system, the container usually doesn’t come with the tzdata package and the only way for it to use your timezone is to use the information from the host system by mounting the 2 files.

However, if you don’t mount these files, the container will usually run in UTC and won’t observe DST. So, all the times in log files and everything regarding time will be in UTC.

BCsven@lemmy.ca on 08 Dec 03:10 next collapse

For a lot of applications the correct time may not matter but for something like homeassstant or syncthing you want your container time to be correct on a restart

Coelacanthus@lemmy.kde.social on 11 Dec 00:03 collapse

Without it, the program just fallback to UTC. /etc/localtime and $TZ are two ways to specify timezone for programs. The program look up the later first, if it’s empty and then use /etc/localtime. In theory, $TZ has better performance because of the glibc will not look up the last modified time of /etc/localtime when every calling of localtime lookup.