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

7

118

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.

Rick answered 2/4, 2014 at 2:51 Comment(0)
A
104

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

Ashe answered 19/10, 2014 at 18:44 Comment(4)
Thank you so much. This was the only thing that worked.Burson
See the purported fix/mitigation: github.com/boot2docker/boot2docker/pull/661Pagoda
Note that for Docker for OSX Beta, restarting the container didn't work me, and neither did recreating it. Instead, I had to restart Docker itself (from the menubar icon).Display
I wrote a CLI tool based on this answer to synchronize time on docker-machine VMs every 5 minutes. Works on my OSX El Capitan and Windows 7 ProOpus
P
54

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
Poulenc answered 2/4, 2014 at 18:42 Comment(5)
I don't know if the original question was more about timezones (e.g. making sure that the container will honor DST and timezone changes) or accurate time keeping (e.g. making sure that the container clock will not drift). If it's about timezones, that answer is perfect. If it's about clock drift, you don't have to worry: the clock of the container is the same as the clock of the host (except that the container cannot change it, except when it's running in --privileged mode).Quito
@Quito agreed; I should have included a call to date on the host machine in my MWE, as it is otherwise perhaps unclear that the container gets its time from the host.Poulenc
setup mount namespace mounting /etc/localtime into /mnt/sda1/var/lib/docker/aufs/mnt/.../etc/localtime not a directoryHokanson
This does not appear to work per docker 1.3 on OSX. Same error as Brian Tingle.Ashe
@Ashe boot2docker can only mount subdirectories under /Users by defaultDrusi
K
54

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"
Kaule answered 5/8, 2014 at 2:25 Comment(2)
Option 2 worked for me. I actually like that is is more explicit than the other options.Mezereum
Thank you option 2 also worked for me. I get an error on my Mac with option 1 as I have no etc/localtime folder.Scarab
C
28

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.

Cromagnon answered 30/6, 2016 at 21:53 Comment(1)
This should be the newly accepted answer for people using the new RCs without the intermittent docker-machineCree
A
3

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
Adaadabel answered 28/4, 2018 at 23:19 Comment(0)
C
2

docker-compose usage:

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

Look at this link to demonstrate an example.

Cope answered 21/8, 2018 at 5:55 Comment(0)
G
1

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
Glidewell answered 26/10, 2020 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.