How to make sure docker's time syncs with that of the host?

前端 未结 17 1541
北恋
北恋 2020-12-02 07:10

I have dockers running on Linode servers. At times, I see that the time is not right on the dockers. Currently I have changed the run script in every docker to include the f

17条回答
  •  悲哀的现实
    2020-12-02 07:27

    This is what worked for me with a Fedora 20 host. I ran a container using:

    docker run -v /etc/localtime:/etc/localtime:ro -i -t mattdm/fedora /bin/bash
    

    Initially /etc/localtime was a soft link to /usr/share/zoneinfo/Asia/Kolkata which Indian Standard Time. Executing date inside the container showed me same time as that on the host. I exited from the shell and stopped the container using docker stop .

    Next, I removed this file and made it link to /usr/share/zoneinfo/Singapore for testing purpose. Host time was set to Singapore time zone. And then did docker start . Then accessed its shell again using nsenter and found that time was now set to Singapore time zone.

    docker start 
    docker inspect -f {{.State.Pid}} 
    nsenter -m -u -i -n -p -t  /bin/bash
    

    So the key here is to use -v /etc/localtime:/etc/localtime:ro when you run the container first time. I found it on this link.

    Hope it helps.

提交回复
热议问题