Docker 运行的容器时间不对,怎么解决!


rt
已邀请:

小飞侠 - TenxCloud合伙人

赞同来自: lincoln_alex chuckzhang 徐磊 -无夏之年- dracarys


启动容器时候加一下 -v /etc/localtime:/etc/localtime,容器里面就会跟host一个时区了
也可以在Dockerfile里面指定。

智云同学 - 装逼招雷劈,人帅被人欺

赞同来自: lincoln_alex 郑伟-风刃


是不是from的基础镜像时间不对,在Dockerfile里面指定一下时区

michaelzhang

赞同来自: dotNetDR_


建議在構建的時候可以加上一條指定時區的設置:
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

landa_x

赞同来自:


请问,如何修改容器时间,不是时区,是随意修改,类似date -s

小飞侠 - TenxCloud合伙人

赞同来自:


应该不行,Time namespace没进入内核,所以没法隔离,找到一些更全面的解释:

short answer first: you can't

long story:
unfortunately you can't cause all containers are lightweight and will "share" the same linux kernel and setting the system time/date is a (privileged) kernel function.

this will work only inside a VM (vmware, kvm etc.) where's a full blown OS running it own kernel.

so you could start a docker container with "- - privileged=true" to manipulate the time/date inside this container.

but ... this will then afflect the hosts settings also, which means if you set a new time/date inside a privileged container it will modify the hosts time/date also and therefore set the time in all running containers to this new value.

allansun - 无名小虾

赞同来自:


我觉得你应该还是想要修改时区就对了。

在Dockerfile里增加
RUN echo "Europe/London" > /etc/timezone; 


就能更改系统成英国时区。

这里有Linux系统的时区列表 : https://en.wikipedia.org/wiki/ ... zones

要回复问题请先登录注册