Ubuntu在Docker容器中的timedatectl失败

19

我在一个运行在 macOS 上的 Docker 容器中使用 Ubuntu 16.04 LTS。日期/时间偏移了大约四天。

$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
$ date
Sun May  7 05:57:21 UTC 2017

生效日期为2017年5月11日06:17:13 UTC。

我想修复这个问题(查看),但我甚至无法运行timedatectl

$ timedatectl status
Failed to create bus connection: No such file or directory

我该如何修复这个问题?


请查看以下帖子是否有所帮助 - https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes 或者 https://dev59.com/y1kS5IYBdhLWcg3wMj5I - Rao
不,问题不在于时区不正确,我对UTC很满意。 ls -l /etc/localtime 显示它正确地指向 /usr/share/zoneinfo/Etc/UTC。因此运行 dpkg-reconfigure -f noninteractive tzdata 没有任何效果,日期仍然偏差约4天。 - Marcel Stör
在不同的操作系统(Windows/Linux)上获取相同的Ubuntu镜像,显示协调世界时并且正确。希望主机时间没有问题。有什么方法可以重现它吗? - Rao
主机操作系统中的 date 返回正确的值。要进行复制,您可以运行 docker run --rm -ti marcelstoer/nodemcu-build date - Marcel Stör
谢谢,显示正确的 Thu May 11 08:06:23 UTC 2017 - Rao
2个回答

9
回答实际问题(如何在Docker容器中运行timedatectl status时解决“Failed to create bus connection: No such file or directory”错误):
向docker run命令添加以下标志:
--privileged
--volume /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro

如果不使用 --privileged,将出现“无法查询服务器:对等方重置连接”错误。使用 ro 选项似乎可以正常工作。


如果我挂载/run/dbus,我会得到以下错误。"Error starting container cf2e79d525716ac9c926980d16a0753b1b296fd5965d3c58003620d2608656a3: 502 Server Error: Bad Gateway ("Mounts denied: \r\nThe path /run/dbus/system_bus_socket\r\nis not shared from OS X and is not known to Docker.\r\nYou can configure shared paths from Docker -> Preferences... -> File Sharing.\r\nSee https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info. - Damian O' Neill
2
基础镜像中缺少 dbus,通过 apt-get install dbus 命令解决问题。 - Damian O' Neill
2
有没有不使用特权标志的方法来实现这个?Bitbucket管道不允许在特权模式下运行Docker容器。 - ConorSheehan1
在我的Windows 10上无法工作。 - theerrormagnet

2

时间漂移是由底层主机操作系统引起的,对于Docker而言,它并不是macOS,而实际上是运行在macOS上的Linux虚拟机。这与macOS的睡眠时间有关(例如,当您关闭MacBook盖子时)。显然最近已经修复了这个问题,并且很快就会可用:https://github.com/docker/for-mac/issues/17#issuecomment-300734810


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接