Get the App
SLTechnology News&Howtos  ›  Servers  › 

The method of dealing with the problem of time Zone in docker

Shulou Source: shulou.com Published: 2022-06-02 04:15:26 10月11日 Update

Background

These two days when playing docker, 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

Look at / etc/localtime and find that the default is to point to the Etc/UTC time zone. And the TZ environment variable is not set.

How to deal with the time zone problem in linux?

In fact, all our questions about time zone processing are about dealing with time zones in glibc. The most authoritative document on this issue is the official document of glibc, which describes the handling of time zone issues in the description of TZ environment variables.

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

The parts related 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 that describes time zone-related information.

If the environment variable TZ is not set, the system selects a default value, which in glibc defaults to / etc/localtime. If the TZ environment variable has a value, and the value starts with /, it is the file name of an absolute path, otherwise the file name is / usr/share/zoneinfo/$TZ. The / usr/share/zoneinfo directory contains local time information from all over the world, such as Asia/Shanghai. Generally, the files under this directory are installed by the tzdata package.

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

The method of handling time Zone by docker Container

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

Here is an example of setting the TZ environment variable (which I prefer to do myself, which is more convenient than modifying / etc/localtime).

First of all, we can add ENV TZ=Asia/Shanghai to Dockerfile so that the default TZ environment variable of the image from docker build is the value we want.

Second, we can also use-e TZ=Asia/Shanghai to set the TZ environment variable when the container is pulled up. This setting is dynamic, and we can set different values when the same image is pulled up.

Summary

The time zone processing in docker is actually the time zone processing in glibc. Knowing how to deal with things in glibc, the core is TZ environment variables and / etc/localtime files. It is easy to deal with time problems in docker.

Tags: Time zone variable environment processing file problem container that is method time mirror information content actual actually document time core directory facet Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Microsoft Docker MySQL