Solution to Docker container time zone and time out of sync
- 2020-05-30 21:23:49
- OfStack
Today, a test bug was submitted by the tester during the system integration test. The reason is that there is a deviation between the submitted business data time and the actual time (Beijing time), resulting in statistical anomaly. Since our integration test directly provided the tester with a complete Docker image as the test environment, the reason should be in the container time setting.
After getting the delivered docker image, start it and enter the container console. Use the date command to check that the time is not correct. It is correct to check the host time again, so it must be that the time zone was not synchronized with the host when the container was started. Since the test image was built directly by dockfile, the problem is basically fixed on the dockerfile file.
After opening the dockerfile check, it was found that the synchronization Settings were indeed lost with the host's time zone. Therefore, here is how to add time zone synchronization process record 1, which can be used for reference by students who have encountered similar problems:
Here is an example of docker mirror made by Alpine (also our test mirror) :
1 Dockerfile modify
1.1 add the installation of the tzdata package
The tzdata installation package is installed when the package is installed, and it cannot be cleaned up after a successful build
# Define environment variables
ENV TIME_ZONE Asiz/Shanghai
#dockerfile Increase the order
RUN \
# The installation tzdata The installation package
&& apk add --no-cache tzdata \
1.2 add time zone configuration
After installing this installation package, the configuration information of each time zone will be generated in the directory /usr/share/zoneinfo. There is no timezone and locatime configuration in the directory Alpine. At this time, we need to override the time zone value to timezone and localtime configuration of Alpine.
```
RUN \
...
# The installation tzdata The installation package
&& apk add --no-cache tzdata \
# Set the time zone
&& echo "${TIME_ZONE}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime \
At this point, the modification of dockerfile has been completed.
Host time zone and time check
To check whether the host time and time are correct, use the following command:
```
[root@docker ~]# timedatectl
Local time: Tue 2016-12-13 21:52:13 EST
Universal time: Wed 2016-12-14 02:52:13 UTC
RTC time: Wed 2016-12-14 02:52:13
Time zone: America/New_York (EST, -0500) // The default for the west 5 area
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: no
Last DST change: DST ended at
Sun 2016-11-06 01:59:59 EDT
Sun 2016-11-06 01:00:00 EST
Next DST change: DST begins (the clock jumps one hour forward) at
Sun 2017-03-12 01:59:59 EST
Sun 2017-03-12 03:00:00 EDT
# Is modified to the east 8 area
[root@docker ~]# timedatectl set-timezone Asia/Shanghai
Local time: Wed 2016-12-14 10:53:10 CST
Universal time: Wed 2016-12-14 02:53:10 UTC
RTC time: Wed 2016-12-14 02:53:10
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@docker ~]# date
Wed Dec 14 10:53:49 CST 2016
# Adjust the time
[root@docker ~]# date -s "2016-12-13 21:54:20"
# Time synchronization cannot take effect without execution
[root@docker ~]# clock -w
[root@docker ~]# timedatectl
Local time: Tue 2016-12-13 22:59:44 CST
Universal time: Tue 2016-12-13 14:59:44 UTC
RTC time: Tue 2016-12-13 14:59:44
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
```
Thus the host time zone and time adjustment are completed.
3. Build container tests
Recreate the image based on the dockerfile command above, and launch the container using the RUN command to see the current window time
/ # date
Tue Dec 13 23:01:18 CST 2016
Has been synchronized with the host, the whole adjustment process is over ~~