'How do I change timezone in a docker container?
I am running docker container for my development stack which I pulled from docker-hub, the image is created for a different timezone than where my application is supposed to be deployed.
How do I change timezone in a docker container?
I tried to change the timezone config within the container by running
echo "Africa/Lusaka" > /etc/timezone
and restarted the container but I still get the same timezone.
Solution 1:[1]
There's a few ways to do it .
- You can declare the time zone directly as an environment variable in the docker compose file
environment: - TZ=Asia/Singapore - DEBIAN_FRONTEND=noninteractive
- You can map the container's time zone and local time files to use that of the host machine in the docker compose file
volumes: - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro
I personally prefer to use the second method, in this way , all of my containers will have the same time configuration as my host machine
Solution 2:[2]
Simply change the /etc/localtime to the time zone in the /usr/share/zoneinfo directory.
follow these steps:
first log into bash of your container:
docker exec -u 0 -it mycontainer bash
then remove the symbolic link file (/etc/localtime):
sudo rm -rf /etc/localtime
Identify the timezone you want to configure and create the symbolic link for it:
For instance, I would like to set Asia/Tehran timezone:
ln -s /usr/share/zoneinfo/Asia/Tehran /etc/localtime
Now verify it by:
date
and the output would be your timezone:
Sat Jan 30 14:22:17 +0330 2021
Solution 3:[3]
the best way is to use ENV in your run stage
-e TZ=Africa/Lusaka
and make sure that the package tzdata is present in the Container
Solution 4:[4]
A simpler method would be to add an env var to your deployment:
env:
- name: TZ
value: "Europe/London"
(kubernetes deployment yaml)
Solution 5:[5]
If you have TZ env set correctly and you still get the wrong time, make sure the tzdata system dependency is installed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rugby2312 |
| Solution 2 | Morteza Naeimabadi |
| Solution 3 | LinPy |
| Solution 4 | Al Brain |
| Solution 5 | Blaise |
