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

Blog    

Recommend

Use CSS's clip-path property to display irregular graphics

clip-path CSS properties use clipping to create t...

Implementation of Nginx configuration Https security authentication

1. The difference between Http and Https HTTP: It...

Example code of vue icon selector

Source: http://www.ruoyi.vip/ import Vue from ...

Two practical ways to enable proxy in React

Two ways to enable proxy React does not have enca...

Steps to completely uninstall the docker image

1. docker ps -a view the running image process [r...

Solution to the problem of insufficient storage resource pool of Docker server

Table of contents 1. Problem Description 2. Probl...

What does the legendary VUE syntax sugar do?

Table of contents 1. What is syntactic sugar? 2. ...

Open the Windows server port (take port 8080 as an example)

What is a Port? The ports we usually refer to are...

Getting Started Guide to Converting Vue to React

Table of contents design Component Communication ...

Five things a good user experience designer should do well (picture and text)

This article is translated from the blog Usability...

Detailed explanation of how Node.js handles ES6 modules

Table of contents 1. Differences between the two ...

Front-end state management (Part 1)

Table of contents 1. What is front-end state mana...

Nginx try_files directive usage examples

Nginx's configuration syntax is flexible and ...