Will a docker container auto sync time with its host machine?

TimeTimezoneClockDockerNtp

Time Problem Overview


Do I need a NTP server inside a docker container to periodically sync the time or will the container re-sync time with its host machine? The docker container time zone is correctly set.

Time Solutions


Solution 1 - Time

If you are on OSX running boot2docker, see this issue: https://github.com/boot2docker/boot2docker/issues/290

Time synch becomes an issue because the boot2docker host has its time drift while your OS is asleep. Time synch with your docker container cannot be resolved by running your container with -v /etc/localtime:/etc/localtime:ro

Instead, for now, you have to periodically run this on OSX:

/usr/local/bin/boot2docker ssh sudo ntpclient -s -h pool.ntp.org

Update for users of Kitematic

If you are running Kitematic, which is now the suggested mechanism for getting up and running on Docker in OSX, you will have to periodically run this command:

docker-machine ssh default 'sudo ntpclient -s -h pool.ntp.org'

Or, for older versions of docker

docker-machine ssh dev 'sudo ntpclient -s -h pool.ntp.org'

Update for users of new native Docker for OSX

The new Docker Beta does away with VirtualBox and Docker Machine. The latest builds of docker (currently, 1.12.1-beta25 (build: 11807)) seem to have the ability to detect when there has been a time discontinuity and adjust accordingly. Thus, this should no longer be an issue...hooray!!

Solution 2 - Time

https://github.com/sameersbn/docker-gitlab/issues/77

See sameersbn's answer.

option 1: -v /etc/localtime:/etc/localtime:ro
option 2: -e "TZ=Asia/Shanghai"

Solution 3 - Time

The simplest solution appears to be to run your container with the -v /etc/localtime:/etc/localtime:ro option. Thus:

#run without tz info:
docker run --rm -t -i ubuntu date
Wed Apr  2 18:40:07 UTC 2014
# run with tz info:
docker run --rm -t -i -v /etc/localtime:/etc/localtime:ro ubuntu date
Wed Apr  2 11:40:29 PDT 2014

Solution 4 - Time

On Docker for Mac OS X Beta, I experienced significant drift on the VM, which is based on Alpine Linux. From Alpine Linux FAQ you can synchronize the VM's clock with the following command.

ntpd -d -q -n -p pool.ntp.org

However, getting access to a terminal on the VM is another question, which can be done if you use the screen command.

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

That path is a symlink, which on my system points at /dev/ttys003.

Once you get in, note that the moby login is simply root with no password. After you have finished, CTRL-A, D will disconnect from the screen session.

NOTE: This used to be documented on Docker for Mac Trouble Shooting but that seems to have been taken down. I was lucky enough to be shown it while at Dockercon 2016. It seems Docker is trying to abstract the VM completely out of the experience, which explains why it's no longer documented.

Solution 5 - Time

The current solution for osx time drift on docker (April 2018):

I do have my mac on an NTP server, but this fixed clock drift with containers:

From https://docs.docker.com/docker-for-mac/troubleshoot/#known-issues :

If your system does not have access to an NTP server, then after a hibernate the time seen by Docker for Mac may be considerably out of sync with the host. Furthermore, the time may slowly drift out of sync during use. To manually reset the time after hibernation, run:

docker run --rm --privileged alpine hwclock -s

Or, to resolve both issues, you can add the local clock as a low-priority (high stratum) fallback NTP time source for the host. To do this, edit the host’s /etc/ntp-restrict.conf to add:

server 127.127.1.1              # LCL, local clock
fudge  127.127.1.1 stratum 12   # increase stratum

Then restart the NTP service with:

sudo launchctl unload /System/Library/LaunchDaemons/org.ntp.ntpd.plist
sudo launchctl load /System/Library/LaunchDaemons/org.ntp.ntpd.plist

Solution 6 - Time

docker-compose usage:

Add /etc/localtime:/etc/localtime:ro to the volumes attribute.

Look at this link to demonstrate an example.

Solution 7 - Time

If you prefer the TZ solution then you may be surprised to see UTC time displayed despite your request for your own timezone (it's currently 11:09 CDT):

$ docker run --rm -it -e "TZ=America/Chicago" ubuntu date
Mon Oct 26 16:09:04 America 2020

Experimentally, it seems you need the POSIX TZ format:

$ docker run --rm -it -e "TZ=CST6CDT" ubuntu date
Mon Oct 26 11:09:17 CDT 2020

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvanttView Question on Stackoverflow
Solution 1 - TimeesilverView Answer on Stackoverflow
Solution 2 - Timeuser3908675View Answer on Stackoverflow
Solution 3 - TimeshabbychefView Answer on Stackoverflow
Solution 4 - TimeMartin WoolstenhulmeView Answer on Stackoverflow
Solution 5 - TimeSteve KallestadView Answer on Stackoverflow
Solution 6 - TimeBenyamin JafariView Answer on Stackoverflow
Solution 7 - TimedavidvandebunteView Answer on Stackoverflow