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.
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:
|
<<: How InnoDB cleverly implements transaction isolation levels
>>: WeChat applet uses canvas to draw clocks
Table of contents The concept of affairs The stat...
The first method: docker installation 1. Pull the...
This article uses an example to illustrate the ex...
This article mainly introduces how to implement l...
This article shares the installation and configur...
Preface Histogram is a basic statistical informat...
This article shares the specific code of jQuery t...
Introduction: The configuration of Docker running...
<br />It has been no more than two years sin...
Preface meta is an auxiliary tag in the head area...
Table of contents Business Background Using Techn...
Table of contents 1. v-on directive 1. Basic usag...
I wrote some Qt interface programs, but found it ...
1. Log in to mysql: mysql -u root -h 127.0.0.1 -p...
Table of contents Preface Browser compiled versio...