How to deal with time zone issues in Docker

How to deal with time zone issues in Docker

background

When I was using Docker these two days, I found that after my container was started, the output time zone of date -R was UTC, which was always 8 hours behind Beijing time.


Standard image


Time zone is UTC

Checking /etc/localtime, I found that the default time zone is Etc/UTC. And the TZ environment variable is not set.


How to deal with time zone issues in Linux

In fact, all of our time zone handling issues are time zone handling issues in glibc. The most authoritative documentation on this issue is the official glibc documentation, which describes the TZ environment variable and introduces the handling of time zone issues.

https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html#TZ-Variable

The parts that are relevant to us are as follows.

The core meaning is as follows: In glibc, the value of the TZ environment variable is the name of a file whose contents describe time zone related information.

If the TZ environment variable is not set, the system will choose a default value. In glibc, the default value is /etc/localtime. If the TZ environment variable has a value and the value starts with /, it is an absolute path file name, otherwise the file name is /usr/share/zoneinfo/$TZ. The /usr/share/zoneinfo directory contains local time information for various parts of the world, such as Asia/Shanghai. Generally, the files in this directory are installed by the tzdata package.

Following this idea, we force the TZ environment variable to be Asia/Shanghai, and the time zone is correct


Force modification of the /etc/localtime file, the time zone is also correct.

How to handle time zones in Docker containers

According to the above description, there are actually two main ways to set the time zone in a docker container. One is to set the TZ environment variable directly, and the other is to modify the content of /etc/localtime directly (either through a soft link or by directly copying the file) without setting the TZ environment variable.

Here we take setting the TZ environment variable as an example (I prefer to do this myself, it feels more convenient than modifying /etc/localtime).

First, we can add ENV TZ=Asia/Shanghai in the Dockerfile, so that the default TZ environment variable of the image built by Docker is the value we want.


Secondly, we can also use -e TZ=Asia/Shanghai to set the TZ environment variable when pulling up the container. This setting is dynamic. We can set different values ​​for the same image when pulling it up.


Summarize

The time zone processing in docker is actually the time zone processing in glibc. After understanding how glibc handles things, the core is the TZ environment variable and the /etc/localtime file, the time zone processing in docker is simple.

You may also be interested in:
  • Docker time zone issue and data migration issue
  • Docker container time zone adjustment operation
  • The created Docker container time display error/date error/time zone error
  • Detailed explanation of modifying docker time zone and common docker commands
  • How to solve the problem of Docker container time zone and time synchronization
  • The difference between two ways to create a docker image and start the container (summary)
  • Docker container time zone error issue

<<:  How InnoDB cleverly implements transaction isolation levels

>>:  WeChat applet uses canvas to draw clocks

Recommend

In-depth understanding of the role of Vuex

Table of contents Overview How to share data betw...

Detailed tutorial for upgrading zabbix monitoring 4.4 to 5.0

1. Zabbix backup [root@iZ2zeapnvuohe8p14289u6Z /]...

Details of MutationObServer monitoring DOM elements in JavaScript

1. Basic Use It can be instantiated through the M...

Design theory: On the issues of scheme, resources and communication

<br />This problem does not exist in many sm...

A brief analysis of HTML space code

How much do you know about HTML? If you are learni...

Solution to MySQL master-slave delay problem

Today we will look at why master-slave delay occu...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

Detailed explanation of Vue's caching method example

Recently, a new requirement "front-end cache...

How to implement digital paging effect code and steps in CSS

A considerable number of websites use digital pagi...

How to quickly use mysqlreplicate to build MySQL master-slave

Introduction The mysql-utilities toolset is a col...

In-depth understanding of the use of CSS clear:both

clear:both is used to清除浮動This is the impression I...

Linux operation and maintenance basic swap partition and lvm management tutorial

Table of contents 1. Swap partition SWAP 1.1 Crea...

Tools to convert static websites into RSS

<br /> This article is translated from allwe...