Keeping Docker container time in sync with host on OSX

Docker containers on your Mac can get their time skewed over the time your Mac sleeps and wakes up. This happens because Docker doesn't sync container time with host hardware clock on wake up. This can be a nag when you are dealing with an API server like Amazon S3, which requires requests to be signed with time and a very short expiry.

Fortunately, the fix isn't that hard with the sleepwatcher brew.

brew install sleepwatcher  
brew services start sleepwatcher  
echo /usr/local/bin/docker run --rm --privileged \  
    node:argon hwclock -s > ~/.wakeup
chmod +x ~/.wakeup  

What does this do? Using sleepwatcher, schedule a forced hwclock -s sync in containers each time the Mac wakes up from sleep. Here I'm using node:argon as my OS name. Be sure to replace that with the OS image you are using.

comments powered by Disqus