for-mac icon indicating copy to clipboard operation
for-mac copied to clipboard

Time drift on MacOS

Open oojacoboo opened this issue 9 years ago • 57 comments

Version 17.06.2-ce-mac27 (19124)

Can confirm that there is time drift on the Docker client for Mac. I was able to confirm that restarting the Docker client corrects the time issue. Issue is present across all docker containers and docker run busybox date.

There are quite a few reports of this on previous versions. And it was supposedly fixed, but it is not.

The time being off here was only about a minute prior to restarting the Docker client. I cannot say how far it would have been off had the host been put to sleep. In fact, that may have triggered an update and corrected the issue.

It seems the VM clock emulation is just not accurate. Is the VM not using NTP? Can we not have a cron re-sync regularly, even without a Docker client interruption?

oojacoboo avatar Sep 20 '17 19:09 oojacoboo

Time is currently set by 2 mechanisms:

  • NTP via chronyd is supposed to keep things in sync with the host
  • when the host wakes from sleep -- and the clock is too out-of-sync for chronyd to correct -- there's an explicit hwclock -s

I'm not sure what's allowing it to drift in your case. Perhaps the machine wakes, and runs hwclock -s but chronyd has already given up trying to sync the clocks?

When next in this state, could you log into the VM with:

docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh

and then query the NTP state with chronyc e.g.

/ # chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

/ # chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.65.1                  2   6   377    53    +71us[ +120us] +/-   32ms

djs55 avatar Sep 21 '17 09:09 djs55

@djs55 sure - will do. Also, please note that my system never went to sleep. And, I know this to be the case because I just installed OS X 10.13 the day before. But, I also rarely put the system to sleep in general.

oojacoboo avatar Sep 21 '17 13:09 oojacoboo

I'm also seeing this - output captured below:

% docker --version
Docker version 17.09.0-ce, build afdb6d4
% docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh
/ # chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
/ # chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.65.1                  0   9     0     -     +0ns[   +0ns] +/-    0ns

27secs of drift:

%docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/date ; date
Thu Oct  5 22:53:50 UTC 2017
Fri  6 Oct 2017 09:54:17 AEDT

jpz avatar Oct 05 '17 22:10 jpz

Seeing this as well

$ docker --version
Docker version 17.09.0-ce, build afdb6d4

$ date
Thu Oct 26 11:57:14 EDT 2017

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/date
Thu Oct 26 16:01:25 UTC 2017

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /usr/bin/chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /usr/bin/chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.65.1                  0   5     0     -     +0ns[   +0ns] +/-    0ns

ddollar avatar Oct 26 '17 15:10 ddollar

I'm also seeing considerable drift of several minutes on macOS 10.13.1 (17B46a). My system is always on, I only set the screens to sleep when I'm away from the computer.

$ docker --version
Docker version 17.09.0-ce, build afdb6d4

$ uname -a
Darwin couchbook.local 17.2.0 Darwin Kernel Version 17.2.0: Sun Oct  1 00:46:50 PDT 2017; root:xnu-4570.20.62~10/RELEASE_X86_64 x86_64

$ last reboot | head -n1
reboot    ~                         Sat Oct 28 20:24

$ uptime
16:17  up 5 days, 20:53, 1 user, load averages: 2,92 3,54 3,87

$ date -u ; docker run busybox date
Fr  3 Nov 2017 15:20:43 UTC
Fri Nov  3 15:26:26 UTC 2017

$ date -u; docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /sbin/hwclock -r
Fr  3 Nov 2017 15:25:11 UTC
Fri Nov  3 15:25:12 2017  0.000000 seconds

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /usr/bin/chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /usr/bin/chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.65.1                  0  10     0     -     +0ns[   +0ns] +/-    0ns

I also noticed that the chrony drift file can't be written because of a missing directory:

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh

# cat /etc/chrony/chrony.conf
server 192.168.65.1 trust iburst minpoll 2
makestep 0.5 -1
keyfile /etc/chrony/chrony.keys
driftfile /var/lib/chrony/chrony.drift
cmdport 0

# stat /var/lib/chrony/chrony.drift
stat: can't stat '/var/lib/chrony/chrony.drift': No such file or directory

# ls /var/lib
dhcpcd  docker  misc

This probably doesn't matter much, since it only affects initial clock drift correction after chronyd restarts.

One interesting thing to note though is that the hwclock in the VM is not drifting, but the system time is drifting from RTC. This indicates that the clock provider is doing a bad job.

Currently it is using the TSC time source, it might make sense to experiment with other providers like hpet or acpi_pm. There are various post on the web with TSC causing trouble on HyperV, os it is likely it affects other hypervisors as well.

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh

# cat /sys/devices/system/clocksource/clocksource0/current_clocksource
tsc

# cat /sys/devices/system/clocksource/clocksource0/available_clocksource
tsc hpet acpi_pm

I'll try experimenting with hpet first and see if that helps and try acpi_pm next.

The clocksource can be changed at runtime:

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh

# echo hpet > /sys/devices/system/clocksource/clocksource0/current_clocksource

# cat /sys/devices/system/clocksource/clocksource0/current_clocksource
hpet

If your VMs system time currently drifts from the rtc, sync it back after switching the clocksource:

# hwclock --hctosys

I found lots of useful info under the following links:

felixbuenemann avatar Nov 03 '17 16:11 felixbuenemann

Same here, with a drift of 16 minutes. Fixed by restarting Docker.

$ docker --version                                                                                                                                                               
Docker version 17.09.0-ce, build afdb6d4

$ docker run --rm --net=host --pid=host --privileged -it justincormack/nsenter1 /bin/sh
/ # chronyc activity
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

/ # chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 192.168.65.1                  0  10     0     -     +0ns[   +0ns] +/-    0ns

/ # date
Thu Nov 23 02:35:33 UTC 2017
/# exit

$ date                                                                                                                                                                           
Thu 23 Nov 2017 10:19:57 AWST

OscarBarrett avatar Nov 23 '17 02:11 OscarBarrett

Btw. none of the available clocksources fixed the time drift problem for me.

felixbuenemann avatar Nov 23 '17 19:11 felixbuenemann

I'm seeing a pretty consistent drift of about 1-2ms per second. This is pretty consistent. Resetting the clock time just changes the time, it doesn't address the drift.

ggascoigne avatar Dec 07 '17 20:12 ggascoigne

Same problem on Version 17.09.1-ce-mac42 (21090), Channel: stable, 3176a6af01.

My PostgreSQL container's clock gains several seconds in the future every time I wake up my MacBook Pro from sleep. I love Docker, but having to restart it so often is really annoying...

Let me know if I can help in any way.

adrienjoly avatar Dec 21 '17 10:12 adrienjoly

@adrienjoly - here is a tip: docker run --rm --privileged alpine hwclock -s will reset the time in the docker server. No need to stop and restart.

buchs avatar Dec 23 '17 21:12 buchs

@buchs Thanks for the tip! But isn't there a risk of altering data integrity of my PostgreSQL database running on that container, if I change the time without rebooting?

adrienjoly avatar Dec 24 '17 11:12 adrienjoly

Hmm, interesting question. If you are timestamping entries, and you turn the docker server clock back, you might have issues. What else can you do in a database to cause problems? Constraints based on time? Maybe the log would turn out funny, if you needed to do a recovery. It all depends on what and how you use the database, I suppose.

kbuchsicf avatar Dec 31 '17 02:12 kbuchsicf

You're right, changing the time will cause wrong timestamps in our data. That's not that bad for a development environment. I was mostly worried about PostgreSQL's internal behavior when changing the time back to the past. (i.e. impact on caching, handling of atomic transactions, etc...)

adrienjoly avatar Jan 03 '18 09:01 adrienjoly

I'm seeing the same issue using a Python image. Note that some servers refuse to authenticate my client due to the time disparity! At present a 13 second disparity:

$ docker --version
Docker version 17.12.0-ce, build c97c6d6

$ docker run -it --rm python:3-jessie python -c 'import time;print(time.time())' && python3 -c 'import time;print(time.time())'
1516141290.7820508
1516141277.759674

The hwclock workaround does work for me.

hughsw avatar Jan 16 '18 22:01 hughsw

My clock is drifting roughly 2.5 seconds every minute. hwclock can sync it back up, but resetting, reinstalling, nothing seems to resolve the persistent drift.

$ docker --version
Docker version 17.12.0-ce, build c97c6d6

ntodd avatar Jan 20 '18 01:01 ntodd

Here is the bash script I use on my Mac to restart Docker when needed:

echo "Stopping Docker containers..."
docker-compose down

echo "Stopping Docker gracefully..."
test -z "$(docker ps -q 2>/dev/null)" && osascript -e 'quit app "Docker"'

echo "Starting Docker..."
open --background -a Docker

echo "Waiting 30 seconds..."
sleep 30

echo "Starting Docker containers..."
docker-compose up -d

adrienjoly avatar Jan 22 '18 09:01 adrienjoly

Also seeing very heavy drift.

Docker version 18.01.0-ce, build 03596f5
High Sierra 10.13.2 (17C2120)

thinkspill avatar Jan 24 '18 21:01 thinkspill

Recent versions of Docker for Mac have removed easy access to the "restart" button which was useful for fixing this

ddollar avatar Jan 24 '18 23:01 ddollar

After an upgrade from 17.09.0-ce-mac35 (19611) to 17.12.0-ce-mac49 (21995) yesterday, I've started noticing heavy drifting on Mac OS 10.11.6. The only other change was that the host machine was rebooted just before the upgrade (but no OS patches applied).

I'm developing a time sensitive application which immediately starts triggering protection mechanisms if time is more than 5 seconds away from an external source. Today it has happened twice that this has kicked in. It has never happened before (for several versions back).

It could be fixed by running hwclock -s as shown in earlier posts, but this absolutely seems like some kind of regression.

Edit: First noticed it ~09:25 today, at which time it had ~30s drift, corrected with hwclock -s Now at ~14:30 (approx 5h) it had accumulated another 6s of drift. Machine has been on all the time, docker VM as well. Containers have been restarted & recreated a few times.

stromnet avatar Jan 25 '18 13:01 stromnet

I upgraded another computer that previously wasn't showing drift to 17.12.0-ce-mac, and then it too started showing drift.

It's drifting in a really strange way too. Try this:

  1. Sync your clock with docker run --rm --privileged alpine hwclock -s
  2. Open two terminal windows side by side, one on host, one in any container.
  3. Run this in each terminal, starting them as closely together as possible:
while true; do date; sleep 10; done
  1. Initially you should see each date output at the same time, with matching times, within a second or so, depending on how quickly you started each command.

  2. On my system, here's where it gets weird. The outputs will slowly become out-of-sync as to when the output appears, but the date/time that is output always matches!

For example, after a while my host will output: Thu Jan 25 10:00:24 PST 2018 ... and then some seconds later the container will output: Thu Jan 25 10:00:24 PST 2018

Same times, but the container is delayed ever longer as time goes on. The times will always match, but the container falls further and further into the past.

It seems like time is moving more slowly in Containerlandia.

thinkspill avatar Jan 25 '18 18:01 thinkspill