今天发现日志服务器采集到的某一个系统的日志时间不对,和实际的时间相差了8个小时,我们估计问题是出在容器的时区的设置上。
1. 进入docker 容器后,查看一下时间:
#进入容器docker exec -it xxxx /bin/bash #查看时间bash-4.4# dateMon Feb 1301:32:02 UTC 2023
果然时区是UTC时区
2. 查看一下docker中操作系统的版本,是ununtu 20.04
bash-4.4# cat /proc/versionLinux version 5.4.0-126-generic (buildd@lcy02-amd64-072) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #142-Ubuntu SMP Fri Aug 26 12:12:57 UTC 2022
3. 再检查相应的docker的Dockerfile
FROM anapsix/alpine-java:8u202b08_jdk_unlimited RUN mkdir-p /app/log WORKDIR /app ADD ./abc.jar ./abc.jar ENTRYPOINT ["/bin/sh", "-c", "java -jar abc.jar"]
## 解决问题
1. 对Dockerfile进行修改,增加"时区配置"后如下:
FROM anapsix/alpine-java:8u202b08_jdk_unlimited ENV TZ Asia/Shanghai RUN ln-sf /usr/share/zoneinfo/${TZ} /etc/localtime RUN echo${TZ} > /etc/timezone RUN mkdir-p /app/log WORKDIR /app ADD ./abc.jar ./abc.jar ENTRYPOINT ["/bin/sh", "-c", "java -jar abc.jar"]
**发现还是不好使,时区还是原来的UTC时区!!!**
2. 检查后,发现原来缺少tzdata包
于是对Dockerfile进行修改,"安装tzdata包"后如下:
FROM anapsix/alpine-java:8u202b08_jdk_unlimited RUN apk add --no-cache tzdata ENV TZ Asia/Shanghai RUN ln-sf /usr/share/zoneinfo/${TZ} /etc/localtime RUN echo${TZ} > /etc/timezone RUN mkdir-p /app/log WORKDIR /app ADD ./abc.jar ./abc.jar ENTRYPOINT ["/bin/sh", "-c", "java -jar abc.jar"]
3. 开始构建
sudo docker run --name cloud-render-oss-v3 -dit-p8000:8000cloud-render-oss:v3
Step 1/13 : FROM anapsix/alpine-java:8u202b08_jdk_unlimited ---> 4357b436992a Step 2/13 : MAINTAINER abc:1.0 ---> Using cache ---> 7b43d961907c Step 3/13 : RUN apk add --no-cache tzdata ---> Running in db41073e58ab fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz (1/1) Installing tzdata (2020a-r0) Executing busybox-1.28.4-r3.trigger OK: 25 MiB in24 packages Removing intermediate container db41073e58ab ---> e1cbf2ea9881 Step 4/13 : ENV TZ Asia/Shanghai ---> Running in d8689874d96d Removing intermediate container d8689874d96d ---> b91855f5ae60 Step 5/13 : RUN echo${TZ}---> Running in e92b6f6aab22 Asia/Shanghai Removing intermediate container e92b6f6aab22 ---> 6eb50e8003f3 Step 6/13 : RUN ln-sf /usr/share/zoneinfo/${TZ} /etc/localtime ---> Running in 9bd7a1005cfd Removing intermediate container 9bd7a1005cfd ---> 49270005a068 Step 7/13 : RUN echo${TZ} > /etc/timezone ---> Running in fdccfb507a2c Removing intermediate container fdccfb507a2c ---> d573b3b66e4f
4. 再次进入容器,查看时间
bash-4.4# dateMon Feb 1309:50:49 CST 2023
大功告成!