Solve the difference between the start time of the docker deployment springboot project and the host machine by 8 hours

Description of the problem : When docker deploys the springboot project, it clearly specifies the time zone for mirroring and starting the container, but the time of entering the docker system is still wrong.
Dockerfile:

# 基础镜像
FROM openjdk:8
#申明一个环境变量
#ENV HOME_PATH /home
#指定容器启动时,执行命令会在该目录下执行
#WORKDIR $HOME_PATH
#应用构建成功后的jar复制到容器指定目录下
ENV TZ="Asia/Shanghai"
COPY . .
ADD target/test.jar test.jar
#容器启动时执行的命令
ENTRYPOINT ["java","-jar","-Xms512m","-Xmx512m","-Xss256k","test.jar"]

Docke-compose:

version: '3'
services:
  test-service:
    # 指定容器名称
    container_name: test
    # 重启机制
    restart: always
    image: test:v2.0.3
    volumes:
      # 挂载日志和时区
    - ./log:/log
    - /etc/timezone:/etc/timezone:ro
    - /etc/localtime:/etc/localtime:ro
    ports:
    - "39084:39084"
    environment:
      # 指定时区
      - TZ="Asia/Shanghai"

Host time:

ubuntu@VM-0-12-ubuntu:$ date -R
Mon, 28 Nov 2022 17:55:52 +0800

Enter the docker machine to see the time

docker exec -it 容器名 date -R
Mon, 28 Nov 2022 09:58:08 +0000

Found a difference of 8 hours.

Solution:

When starting the java project add-Duser.timezone=GMT+08参数,指定时间。

具体Dockerfile

# 基础镜像
FROM openjdk:8
#申明一个环境变量
#ENV HOME_PATH /home
#指定容器启动时,执行命令会在该目录下执行
#WORKDIR $HOME_PATH
#应用构建成功后的jar复制到容器指定目录下
ENV TZ="Asia/Shanghai"
COPY . .
ADD target/test.jar test.jar
#容器启动时执行的命令
ENTRYPOINT ["java","-jar","-Xms512m","-Xmx512m","-Xss256k","-Duser.timezone=GMT+08","test.jar"]

docker logs -f container name to view the project time and found that it is normal.

2022-11-28 17:56:54.968  INFO 1 [http-nio-39084-exec-1]{magenta} --- [io-39084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2022-11-28 17:56:54.987  INFO 1 [http-nio-39084-exec-1]{magenta} --- [io-39084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
2022-11-28 17:56:55.018  INFO 1 [http-nio-39084-exec-1]{magenta} --- [io-39084-exec-1] c.m.o.lims.service.filter.LoginFilter    : --------------> request method : GET
2022-11-28 17:56:55.018  INFO 1 [http-nio-39084-exec-1]{magenta} --- [io-39084-exec-1] c.m.o.lims.service.filter.LoginFilter    : --------------> request url : /api/cxmxv1/excel/downloadMasterExcel

Guess you like

Origin blog.csdn.net/weixin_45000409/article/details/128084353