Docker timezone configuration in Linux
Happy devOps
Background
By default, Linux and Docker use UTC timezone. We need to manually configure the timezone in these environments. In this post I’m gonna discuss about configuring Linux and Docker container timezones. All the source codes which related to this post available in gitlab. Please clone the repo and container the post.
Linux timezone
We can configure the Linux timezone with timedatectl
command. Following is the way to do that. Changing Linux machine timezone does not affects to the Docker containers timezone. We need to manually configure the timezone in Docker containers.
Docker timezone
In order to set the timezone in Docker container we can set the TZ
environment variable with the timezone inside the Docker container. To use the TZ environment variable we need to install the tzdata
package in docker container. Here I have dockerized a golang based REST API with installing tzdata
package. Following is the Dockerfile.
When running the service I need to set the TZ environment variable value with the timezone. Following is the way to set it with docker-compose.yml
.
If we run the service without setting TZ variable docker will use default UTC timzone. After setting the TZ and restart the service it will use the given timezone.
Reference
- https://medium.com/developer-space/be-careful-while-playing-docker-about-timezone-configuration-e7a2217e9b76
- https://linuxacademy.com/blog/linux/changing-the-time-zone-in-linux-command-line/
- https://linuxize.com/post/how-to-set-or-change-timezone-in-linux/
- https://www.tecmint.com/set-time-timezone-and-synchronize-time-using-timedatectl-command/