容器默认会使用 UTC 时区( Universal Time Coordinated 通用协调时,即格林尼治时间),如:
$ docker exec 2ec7378b902a date
Thu Sep 12 06:13:43 UTC 2019
$ date
2019年 09月 12日 星期四 14:13:46 CST
容器中是 UTC 时区,宿主机使用的是 CST 时区(China Standard Time 中国标准时间,即东八区时间,领先 UTC 8个小时)
解决:
在容器配置文件中 volumes: 下面增加 -/etc/localtime:/etc/localtime,重启容器即可
volumes:
- /etc/localtime:/etc/localtime
重启后都为 CST 时区时间:
$ docker exec ff3c224ccc43 date
Thu Sep 12 14:19:40 CST 2019
$ date
2019年 09月 12日 星期四 14:19:43 CST