Docker容器、Tomcat应用日志时间不一致如何解决

2022-06-07 08:37:17 浏览数 (1)

客户提问docker容器时间和tomcat应用不一致如何解决。

一、问题情况如下:容器日志显示时间,差了8个小时

  • 错误时间
  • 正确时间

二、解决方法:

  • 1.解决docker容器与宿主机时间不一致问题:在启动容器时,把系统时间挂载到容器内,添加如下参数 -v /etc/localtime:/etc/localtime:ro //ro表示只读
  • 2.解决docker 容器与tomcat 应用打印日志时间不一致问题:在tomcat/bin/catalina.sh文件中修改tomcat JAVA_OPTS,添加如下内容
代码语言:javascript复制
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8  -Duser.timezone=GMT 08

将配置文件,加载至容器

代码语言:javascript复制
    - './config/catalina.sh:/usr/local/tomcat/bin/catalina.sh'

执行命令:重新加载卷

登录容器查看日志信息:

代码语言:javascript复制
docker exec -it  2cbce5974658  /bin/bash  # 登录容器
root@2cbce5974658:/usr/local/tomcat# date
Wed Aug 21 15:35:38 CST 2019
root@2cbce5974658:/usr/local/tomcat# tail -f logs/catalina.2019-08-21.log 
Aug 21, 2019 3:35:19 PM org.apache.catalina.startup.TldConfig execute
INFO: 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 <init>
INFO: ProcessManager implementation is LinuxProcessManager
Aug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.OfficeProcess prepareInstanceProfileDir
WARNING: profile dir '/usr/local/tomcat/temp/.jodconverter_socket_host-127.0.0.1_port-8100' already exists; deleting
Aug 21, 2019 3:35:32 PM org.artofsolving.jodconverter.office.OfficeProcess start
INFO: 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 start
INFO: started process; pid = 81
tail: 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方式获取的容器标准时间有八个小时时间差。也就是容器时间和系统时间一致,但是部署的应用和容器时间不一致。

0 人点赞