问题
Giving I already changed the timezone of docker container correctly. Do I need to install a NTP server inside the docker container to periodically sync the time or the container will sync the time from its host machine?
回答1:
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!!
回答2:
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"
回答3:
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
回答4:
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.
回答5:
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
回答6:
docker-compose usage:
Add /etc/localtime:/etc/localtime:ro
to the volumes
attribute.
Look at this link to demonstrate an example.
来源:https://stackoverflow.com/questions/22800624/will-docker-container-auto-sync-time-with-the-host-machine