Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report