【问题标题】:Docker logs - Synchronize Time from Host to ContainerDocker 日志 - 将时间从主机同步到容器
【发布时间】:2021-01-13 11:05:15
【问题描述】:

我尝试在运行时将时区从主机同步到容器:

docker run -v $(pwd)/Data:/code/Data -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro --restart unless-stopped intermediateservice

这在运行 docker logs 命令时似乎不起作用:

docker logs -f -t zen_blackwell |tee output.log

产生一个时间戳(大约落后 2 小时):

2021-01-13T10:43:22.372893697Z Ready...

这是不正确的,因为运行 timedatectl 命令检查主机上的当前时间(Ubuntu 18.04 LTS - Bionic Beaver)会产生:

timedatectl
                      Local time: Wed 2021-01-13 12:51:00 SAST
                  Universal time: Wed 2021-01-13 10:51:00 UTC
                        RTC time: Wed 2021-01-13 10:51:02
                       Time zone: Africa/Johannesburg (SAST, +0200)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

我在这里错过了什么?

【问题讨论】:

    标签: docker ubuntu docker-compose dockerfile devops


    【解决方案1】:

    你可以看看this answer

    For the docker run -e TZ={timezone} workaround to work tzdata of course has to be installed in the container you’re trying to run.
    What I was initially looking for (Docker container automatically having same timezone as host system) can be achieved through an ugly hack in the run script.
    It can query geoip.ubuntu.com 172 (or any other geo IP database) once it is started on the new network and then set the server’s timezone based on the response: 
    https://askubuntu.com/a/565139
    

    这里给出了askubuntu代码sn-p:

    echo "Setting TimeZone..."
    export tz=`wget -qO - http://geoip.ubuntu.com/lookup | sed -n -e 's/.*<TimeZone>\(.*\)<\/TimeZone>.*/\1/p'` &&  timedatectl set-timezone $tz
    export tz=`timedatectl status| grep Timezone | awk '{print $2}'`
    echo "TimeZone set to $tz"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2022-10-19
      • 2017-04-26
      • 2014-05-13
      • 2016-12-06
      相关资源
      最近更新 更多