Docker container time zone error issue

Docker container time zone error issue

background

Using the node-schedule scheduled task library, I wrote a script to automatically send emails at 7 am every day, and the email will get the date of the day.

question

The email was received at 3pm instead of 7am. I guess it was due to the time zone setting.

Problem analysis and solutions

After investigation, it was found that the node-schedule library does not support the selection of time zone, so it is sent according to the global standard time UTC by default. The time we usually pass in is CST, the Shanghai time zone in China, which is eight hours different.
Changed the scheduled task library to use node-schedule-tz, supports selecting the CST time zone, and uses the corntab time format

let j = schedule.scheduleJob('name',"0 7 * * *",'Asia/Shanghai', function () {

  console.log("Execute task");

  getAllDataAndSendMail();

});

New Problem

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

Problem analysis and solutions

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

let today = new Date()

Because I have printed today's log, check the log

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

It was found that the email sent at 7 am was received today at 23:00 the previous day, which is also 8 hours different. Use the following command to enter the docker container to check the time

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

It is indeed UTC global standard time, which means that the time of sending the email has indeed been changed back, but the time obtained 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 as above and find that it has been changed back to CST. There should be no problem.

This is the end of this article about the docker container time zone error problem. For more related docker time zone error content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker time zone issue and data migration issue
  • Docker container time zone adjustment operation
  • How to deal with time zone issues in Docker
  • 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)

<<:  Bootstrap 3.0 study notes grid system principle

>>:  MySQL database index order by sorting detailed explanation

Recommend

Shell script nginx automation script

This script can satisfy the operations of startin...

CSS code abbreviation div+css layout code abbreviation specification

Using abbreviations can help reduce the size of yo...

Detailed explanation of the marquee attribute in HTML

This tag is not part of HTML3.2 and is only suppo...

HTML table markup tutorial (14): table header

<br />In HTML language, you can automaticall...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

Example code for css flex layout with automatic line wrapping

To create a flex container, simply add a display:...

Detailed analysis of GUID display issues in Mongodb

Find the problem I recently migrated the storage ...

Why should css be placed in the head tag

Think about it: Why should css be placed in the h...

JavaScript Canvas implements Tic-Tac-Toe game

This article shares the specific code of JavaScri...

Detailed process of compiling and installing Storm on Kylin V10 server

1 Introduction Apache Storm is a free, open sourc...