码头容器会自动与主机同步时间吗?

我是否需要在一个 docker 容器内部有一个 NTP 服务器来定期同步时间,或者容器是否会与其主机重新同步时间?码头集装箱时区设置正确。

99220 次浏览

The simplest solution appears to be to run your container with the -v /etc/localtime:/etc/localtime:ro option. Thus:

#run without tz info:
docker run --rm -t -i ubuntu date
Wed Apr  2 18:40:07 UTC 2014
# run with tz info:
docker run --rm -t -i -v /etc/localtime:/etc/localtime:ro ubuntu date
Wed Apr  2 11:40:29 PDT 2014

Https://github.com/sameersbn/docker-gitlab/issues/77

看看 Sameersbn 的答案。

option 1: -v /etc/localtime:/etc/localtime:ro
option 2: -e "TZ=Asia/Shanghai"

If you are on OSX running boot2docker, see this issue: https://github.com/boot2docker/boot2docker/issues/290

时间同步成为一个问题,因为 boot2docker 主机有它的时间漂移,而您的操作系统是睡眠的。使用 -v /etc/localtime:/etc/localtime:ro运行容器无法解决与 Docker 容器的时间同步问题

相反,现在,你必须定期在 OSX 上运行它:

/usr/local/bin/boot2docker ssh sudo ntpclient -s -h pool.ntp.org

风电设备用户最新情况

If you are running 风筝, which is now the suggested mechanism for getting up and running on Docker in OSX, you will have to periodically run this command:

docker-machine ssh default 'sudo ntpclient -s -h pool.ntp.org'

Or, for older versions of docker

docker-machine ssh dev 'sudo ntpclient -s -h pool.ntp.org'

OSX 新本地 Docker 用户更新

新的 Docker 测试版取消了 VirtualBox 和 Docker Machine。最新版本的 docker (目前为1.12.1-beta25(build: 11807))似乎具有检测时间不连续性并进行相应调整的能力。因此,这应该不再是一个问题... 万岁! !

在 Docker for Mac OS X Beta 上,我在基于 Alpine Linux 的 VM 上经历了重大的漂移。从 高山 Linux 常见问题解答可以用以下命令同步 VM 的时钟。

ntpd -d -q -n -p pool.ntp.org

However, getting access to a terminal on the VM is another question, which can be done if you use the screen command.

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

该路径是一个符号链接,在我的系统上指向 /dev/ttys003

进入后,请注意,moby login只是 root,没有密码。完成后,CTRL-A、 D 将断开与屏幕会话的连接。

NOTE: This used to be documented on 解决 Mac 故障的码头工人 but that seems to have been taken down. I was lucky enough to be shown it while at Dockercon 2016. It seems Docker is trying to abstract the VM completely out of the experience, which explains why it's no longer documented.

The current solution for osx time drift on docker (April 2018):

I do have my mac on an NTP server, but this fixed clock drift with containers:

来自 https://docs.docker.com/docker-for-mac/troubleshoot/#known-issues:

If your system does not have access to an NTP server, then after a hibernate the time seen by Docker for Mac may be considerably out of sync with the host. Furthermore, the time may slowly drift out of sync during use. To manually reset the time after hibernation, run:

docker run --rm --privileged alpine hwclock -s

或者,为了解决这两个问题,您可以添加本地时钟作为主机的低优先级(高层)后备 NTP 时间源。为此,请编辑主机的/etc/ntp-limit。Conf 添加:

server 127.127.1.1              # LCL, local clock
fudge  127.127.1.1 stratum 12   # increase stratum

然后使用以下命令重新启动 NTP 服务:

sudo launchctl unload /System/Library/LaunchDaemons/org.ntp.ntpd.plist
sudo launchctl load /System/Library/LaunchDaemons/org.ntp.ntpd.plist

docker-compose usage:

/etc/localtime:/etc/localtime:ro添加到 volumes属性。

请看这个 链接来演示一个示例。

If you prefer the TZ solution then you may be surprised to see UTC time displayed despite your request for your own timezone (it's currently 11:09 CDT):

$ docker run --rm -it -e "TZ=America/Chicago" ubuntu date
Mon Oct 26 16:09:04 America 2020

实验上,你似乎需要 POSIX TZ格式:

$ docker run --rm -it -e "TZ=CST6CDT" ubuntu date
Mon Oct 26 11:09:17 CDT 2020