The simplest way is to add -e option or ENV directive to set TZ environment variable, because debian, ubuntu, etc. already included tzdata.
Someone recommends to mount the timezone from the host:
$ docker run -it --rm \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
debian:bullseye date
Mon Jul 3 19:41:30 CST 2023
This may work, but it is dangerous. Because /etc/localtime is actually a symbol link (in some newer distributions), and Docker by default follow the symbol link. As a result, the timezone data (instead of the symbol link it self) is overridden silently.
$ docker run -it --rm debian:bullseye \
sh -c 'ls -l /etc/localtime && tail -n1 /etc/localtime'
lrwxrwxrwx 1 root root 27 Mar 20 00:00 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
UTC0
$ docker run -it --rm -v /etc/localtime:/etc/localtime debian:bullseye \
sh -c 'ls -l /etc/localtime && tail -n1 /etc/localtime'
lrwxrwxrwx 1 root root 27 Mar 20 08:00 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
CST-8
That is why this does not work:
$ docker run -it --rm \
-v /usr/share/zoneinfo:/usr/share/zoneinfo \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
debian:bullseye date
Mon Jul 3 11:41:59 UTC 2023
Alpine, you need to installtzdatafirst, see here https://github.com/gliderlabs/docker-alpine/issues/136 – Belter Dec 27 '17 at 14:56-v /etc/localtime:/etc/localtime:ro(CentOS) sort of works. Inside container command-line date returns date in the expected timezone format. BUT jenkins running in container thinks timezone is UTC. Why? /etc/localtime is a symlink to ../usr/share/zoneinfo/UTC in built container. The content of the UTC file in container is now the new timezone. But jenkins (and perhaps other java based software) use the name of the symlink which is still "UTC". Searching for solution . . . – gaoithe Jan 21 '19 at 17:29