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.
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.
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.