Option 1. Use the “/etc/localtime" file
Mounting the “localtime" file is the most popular way to change the timezone of containers. Here is an example.
Example: By using host server’s “/etc/localtime" file, change container’s timezone.
sudo docker container run -v /etc/localtime:/etc/localtime:ro ubuntu
I recommend to add read only option “ro" so that containers don’t break host server’s “/etc/localtime" file.
Option 2. Use Environmental Variables
In some cases, with the way of option 1, changing timezone is too late, and timezone can not be changed before the container start. For example, the tomcat container created by the following command starts before timezone is changed and still keeps a default timezone.
sudo docker container run -v /etc/localtime:/etc/localtime:ro tomcat:9.0.30-jdk11-adoptopenjdk-hotspot
How should we do?
Use the environment variable option “-e“. In the following example, “TZ=America/Los_Angeles" is set in the docker container.
sudo docker container run -e TZ=America/Los_Angeles tomcat:9.0.30-jdk11-adoptopenjdk-hotspot
I recommend to use both of Option 1 and 2 like,
sudo docker container run -e TZ=America/Los_Angeles -v /etc/localtime:/etc/localtime:ro tomcat:9.0.30-jdk11-adoptopenjdk-hotspot