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

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!!


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"

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