Time and host vessel time inconsistency

1. Problem Description:

When running the container, the container often found time difference in time and the host eight hours, as follows:

[root@Server1 ~]# date
Sun Feb 16 14:26:50 CST 2020
[root@Server1 ~]# ls -l /etc/localtime
lrwxrwxrwx 1 root root 33 Feb 15 09:32 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
[root@Server1 ~]# docker exec -it redis-test date
Sun Feb 16 06:27:04 UTC 2020
[root@Server1 ~]# docker exec -it redis-test ls -l /etc/localtime
lrwxrwxrwx 1 root root 27 Jan 30 00:00 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC

As can be seen the difference is that the host time zone is CST, CST is China Shanghai Time, eight Eastern time zone; zone while the container is UTC, it refers to the Coordinated Universal Time, the time standard.

2, the solution

Method 1: The host localtime cover to the container

[root@Server1 ~]# docker cp /etc/localtime redis-test:/etc/localtime

[root@Server1 ~]# docker exec -it redis-test date
Sun Feb 16 14:29:58 CST 2020
[root@Server1 ~]# date
Sun Feb 16 14:30:02 CST 2020

Method 2: When running the container specified zone

[root@Server1 ~]# docker run --name redis-test -d -e "TZ=Asia/Shanghai" redis
1a40155f67758554fc2621ef60f4ecb3e251f35c1c1ed1587a4ffed7c48acdfd
[root@Server1 ~]# docker exec -it redis-test date
Sun Feb 16 14:33:57 CST 2020

After the restart of the container, and the host is still time synchronization

[root@Server1 ~]# docker restart redis-test
redis-test
[root@Server1 ~]# docker exec -it redis-test date
Sun Feb 16 14:35:36 CST 2020
[root@Server1 ~]# date
Sun Feb 16 14:35:37 CST 2020

   

 

Guess you like

Origin www.cnblogs.com/sunnynic/p/12316783.html