一、问题情况如下:容器日志显示时间,差了8个小时
- 错误时间
- 正确时间
二、解决方法:
- 1.解决docker容器与宿主机时间不一致问题:在启动容器时,把系统时间挂载到容器内,添加如下参数 -v /etc/localtime:/etc/localtime:ro //ro表示只读
- 2.解决docker 容器与tomcat 应用打印日志时间不一致问题:在tomcat/bin/catalina.sh文件中修改tomcat JAVA_OPTS,添加如下内容
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08
将配置文件,加载至容器
- './config/catalina.sh:/usr/local/tomcat/bin/catalina.sh'
执行命令:重新加载卷
登录容器查看日志信息:
docker exec -it 2cbce5974658 /bin/bash # 登录容器root@2cbce5974658:/usr/local/tomcat# dateWed Aug 21 15:35:38 CST 2019root@2cbce5974658:/usr/local/tomcat# tail -f logs/catalina.2019-08-21.log Aug 21, 2019 3:35:19 PM org.apache.catalina.startup.TldConfig executeINFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.Aug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.ProcessPoolOfficeManager INFO: ProcessManager implementation is LinuxProcessManagerAug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.OfficeProcess prepareInstanceProfileDirWARNING: profile dir '/usr/local/tomcat/temp/.jodconverter_socket_host-127.0.0.1_port-8100' already exists; deletingAug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.OfficeProcess startINFO: starting process with acceptString 'socket,host=127.0.0.1,port=8100,tcpNoDelay=1' and profileDir '/usr/local/tomcat/temp/.jodconverter_socket_host-127.0.0.1_port-8100'Aug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.OfficeProcess startINFO: started process; pid = 81tail: unrecognized file system type 0x794c7630 for ‘logs/catalina.2019-08-21.log’. please report this to bug-coreutils@gnu.org. reverting to polling
总结:
- 1.docker容器和系统时间不一致是因为docker容器的原生时区为0时区,而国内系统为东八区
- 2.容器中运行的tomcat应用打出的日志时间和通过date -R方式获取的容器标准时间有八个小时时间差。也就是容器时间和系统时间一致,但是部署的应用和容器时间不一致。
如果要想要私下交流,可以添加私人QQ:2365823908 ~~