Will docker container auto sync time with the host machine?

前端 未结 7 1972
长情又很酷
长情又很酷 2020-11-28 18:43

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 conta

相关标签:
7条回答
  • 2020-11-28 19:03

    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
    
    0 讨论(0)
  • 2020-11-28 19:04

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

    0 讨论(0)
  • 2020-11-28 19:07

    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"
    
    0 讨论(0)
  • 2020-11-28 19:09

    docker-compose usage:

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

    Look at this link to demonstrate an example.

    0 讨论(0)
  • 2020-11-28 19:10

    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
    
    0 讨论(0)
  • 2020-11-28 19:24

    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
    
    0 讨论(0)
提交回复
热议问题