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

How to solve the problem of docker container time zone error

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

It is believed that many inexperienced people are at a loss about how to solve the problem of docker container time zone error. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

Background

Use node-schedule scheduled task library to write a script to send email automatically and regularly. The email will be sent at 7: 00 a. M. every day, and the date of the day will be obtained in the email.

problem

The email was received at 3: 00 p.m. instead of 7: 00 a.m., presumably because of the time zone setting

Problem analysis and solution

After investigation, it is found that the node-schedule library does not support the selection of time zone, so it is sent by default according to the global standard time UTC. The time we usually pass is the time CST of Shanghai time zone in China, with a difference of eight hours.

Replaced the timing task library, adopted node-schedule-tz timing task library, supported the selection of CST time zone, and adopted corntab time format

Let j = schedule.scheduleJob ('name', "0 7 *",' Asia/Shanghai', function () {console.log ("execute task"); getAllDataAndSendMail ();}); new questions

After changing the time, a new problem arises. The time obtained in the email is yesterday's time, not today's time.

Problem analysis and solution

After thinking about it, there are two places in the code to get the time, one is the email time passed in by the scheduled task library, and the other is the current time in the script.

Let today = new Date ()

Because I have the log of printing today, check the log

Docker logs-f [containerID] # today:2021-11-12T23:00:00.106Z

It was found that today got the email sent at 7: 00 in the morning at 23:00 the day before, with a difference of 8 hours.

Check the time in the docker container using the following command

After entering the container, $docker exec-it [containerID] sh# will change to # # enter the date view time date # Sat Nov 13 05:05:31 UTC 2021

Sure enough, when it is the UTC global standard, it means that the sending time has been changed back, but the time acquired when the code is executed is the current global standard time.

We copy the local time to the container time.

Docker cp / etc/localtime [containerID]: / etc/

Check the time in the container again according to the above, and find that it has been changed back to CST. There should be no problem.

After reading the above, have you mastered how to solve the problem of docker container time zone error? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report