1 . 容器默认创建的主机时区问题, 和主机时间差8小时
myzmac:~ myz$ docker run --name cent1 -it -d centos:7.6.1810 sh
c1469ce1d2a3bad12463a5a22340747d7a82bde98369c792d6d14e5f070d95c9
myzmac:~ myz$
myzmac:~ myz$
myzmac:~ myz$ date
Mon Feb 3 20:42:25 CST 2020
myzmac:~ myz$ docker exec -it cent1 date
Mon Feb 3 12:42:36 UTC 2020
myzmac:~ myz$
解决: 指定时区 -e TZ="Asia/Shanghai"
myzmac:~ myz$ docker run -e TZ="Asia/Shanghai" --name cent2 -it -d centos:7.6.1810 sh
myzmac:~ myz$ docker exec -it cent2 date
Mon Feb 3 20:47:07 CST 2020
2. 上述只是解决了容器中Root 的时区问题, 但容器内其他用户及 数据库的时间有可能仍然不对
[root@pgcls21 /]# date
Tue Dec 17 22:40:44 CST 2019
[root@pgcls21 /]#
[root@pgcls21 /]#
[root@pgcls21 /]# su - postgres
Last login: Tue Dec 17 22:39:20 CST 2019 on pts/5
[postgres@pgcls21 ~]$date
Tue Dec 17 14:40:48 UTC 2019
[postgres@pgcls21 ~]$
解决:
将原有的localtime文件删除
rm -f /etc/localtime
再重新链接时区文件
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
示例:
[root@pgcls21 /]# su - postgres
Last login: Tue Dec 17 22:39:20 CST 2019 on pts/5
[postgres@pgcls21 ~]$date
Tue Dec 17 14:40:48 UTC 2019
[postgres@pgcls21 ~]$
[postgres@pgcls21 ~]$exit
logout
[root@pgcls21 /]#
[root@pgcls21 /]# rm -f /etc/localtime
[root@pgcls21 /]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@pgcls21 /]# su - postgres
Last login: Tue Dec 17 22:40:47 CST 2019 on pts/1
[postgres@pgcls21 ~]$date
Tue Dec 17 22:41:02 CST 2019
网友评论