As we know, many of the Mac users don't shut down their laptops every day. When the work is done we simply close the lid and let the Mac sleep. But when the system wakes up, the time in every docker container will lag behind and starts from the time when the system went to sleep. This is not always a problem but gets really annoying when dealing with services like S3 API that requires the machine to have the correct time.
This is a known issue in Docker for Mac. When Mac wakes up from sleep, Moby VM, the lightweight VM that runs docker engine, doesn't sync the time with host and simply increments from the time when it actually went to sleep. So if we closed the Mac at 10:00 PM, the next day when we open the machine, it's time resume from 10:00 PM.
Some users fix this issue by restarting their machine, while others restart only the docker. Actually, it can be easily fixed by running the following command.
docker run --rm --privileged alpine hwclock -s
This command will start an
alpine container in preveileged mode and will run
hwclock -s command, which will sync the Moby VM time with the hardware clock. If
alpine is not already downloaded, we can use any other image that's already downloaded to save some time.
By using this method, wee will have to run this command every time when there is a time drift. Arunvel Sriram has created a small workaround for this problem by making this command as agent which will run every time the Mac wakes up.
To install the agent:
curl https://raw.githubusercontent.com/arunvelsriram/docker-time-sync-agent/master/install.sh | bash
Now the VM time will automatically get updated whenever the system wakes up from sleep.
Thanks and Enjoy Docker!