首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker日志-将时间从主机同步到容器

Docker日志-将时间从主机同步到容器
EN

Stack Overflow用户
提问于 2021-01-13 19:05:15
回答 1查看 129关注 0票数 0

我尝试在运行时使用以下命令同步主机和容器之间的时区:

代码语言:javascript
复制
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命令时,这似乎不起作用:

代码语言:javascript
复制
docker logs -f -t zen_blackwell |tee output.log

生成时间戳(大约落后2小时):

代码语言:javascript
复制
2021-01-13T10:43:22.372893697Z Ready...

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

代码语言:javascript
复制
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

这里我漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-01-14 16:07:21

你可以看看this answer

代码语言:javascript
复制
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代码片段如下所示:

代码语言:javascript
复制
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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65700649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档