19

I've got Ubuntu 16.04 LTS running in a Docker container (hosted on macOS). The date/time is off by about four days.

$ 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

Effective date is 11 May 2017 06:17:13 UTC.

I wanted to fix this (checking this and this) but I can't even run timedatectl:

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

How do I fix this?

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Please see if this post helps - https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes or http://stackoverflow.com/questions/40234847/docker-timezone-in-ubuntu-16-04-image – Rao May 11 '17 at 06:26
  • Nope, the issue isn't that the timezone is incorrect, I'm fine with UTC. `ls -l /etc/localtime` shows that it correctly points to `/usr/share/zoneinfo/Etc/UTC`. Running `dpkg-reconfigure -f noninteractive tzdata` thus doesn't have any effect, the date is still off by ~4d. – Marcel Stör May 11 '17 at 07:07
  • Picked up the same ubuntu image on different os's(windows / linux), showing UTC and is correct. Hope there is no issue with time in the host machine. Any idea to reproduce it? – Rao May 11 '17 at 07:45
  • `date` on the host OS returns the correct values. To reproduce you can run `docker run --rm -ti marcelstoer/nodemcu-build date`. – Marcel Stör May 11 '17 at 08:01
  • Thank you, shows correct `Thu May 11 08:06:23 UTC 2017` – Rao May 11 '17 at 08:06

2 Answers2

9

To answer the actual question that was asked (how to fix Failed to create bus connection: No such file or directory when running timedatectl status in a Docker container):

Add the following flags to your docker run command:

--privileged
--volume /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro

You need the --privileged, otherwise you get a "Failed to query server: connection reset by peer". The volume flag seems to work fine with ro.

srlm
  • 3,186
  • 2
  • 27
  • 40
  • If I mount /run/dbus I get the following error. "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 Feb 26 '18 at 11:28
  • 2
    dbus missing from the base image, apt-get install dbus resolved problem – Damian O' Neill Feb 26 '18 at 13:35
  • 2
    Is there any way to do this without the privileged flag? bitbucket pipelines don't allow docker containers to be run in privileged mode – ConorSheehan1 Aug 10 '18 at 14:56
  • Does not work for me on Windows 10. – theerrormagnet Feb 10 '22 at 08:00
2

Time drift is caused by the underlying host OS which, for Docker, isn't macOS but actually the Linux VM running on macOS. It's related to macOS sleep times (e.g. when you close the MacBook lid). Apparently it has recently been fixed and should be available soon: https://github.com/docker/for-mac/issues/17#issuecomment-300734810

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198