查看linux系统时区和docker容器时区

date -R                          //查看linux主机时间和时区
date exec [container] date -R    // 查看容器时间和时区

2者的时间差了8个小时

 

解决方案

1、利用Dockerfile创建镜像时。在Dockerfile中加入

ENV TIME_ZONE=Asia/Shanghai 
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone

2、容器创建时。加入时区挂载选项:-v /etc/localtime:/etc/localtime。实例:

docker run -d -p 6379:6379 -v /etc/localtime:/etc/localtime --name test-redis redis

 3、容器已启动时。

docker exec -it container /bin/bash // 进入交互模式,container为容器ID或名称,下同
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
docker restart container // 重启容器
docker exec container date -R // 查看时区

 

以上均亲测试,良心保证,另外网上有搜到docker cp /etc/localtime [container]:/etc/localtime方法,但实际验证没成功,不知为何。

posted on 2019-03-26 23:48  玄同太子  阅读(9687)  评论(2编辑  收藏  举报