简体   繁体   English

与Docker容器共享主机时区

[英]Share host timezone with docker container

I'm trying to sync the timezone of a docker container with my host. 我正在尝试将Docker容器的时区与主机同步。 My host is using ISM and the docker container (using a tomcat image) uses UTC by default. 我的主机正在使用ISM,默认情况下,docker容器(使用tomcat映像)使用UTC。 I've read that we should mount a volume to share the timezone of the host: 我读过我们应该挂载一个卷以共享主机的时区:

$ docker run -t -i -p 8080:8080 -p 8090:8090 -v /etc/localtime:/etc/localtime:ro tomcat:7.0.69-jre8 /bin/bash

After that I can check that the date retrieved is the same as the host: 之后,我可以检查检索到的日期是否与主机相同:

$ date
Fri Jul 22 13:53:45 IST 2016

When I deploy my application and I try to update a date, I can see that the date 22/07/2016 is using my browser timezone, which is the same as the host where the docker container is running. 当我部署应用程序并尝试更新日期时,我可以看到日期22/07/2016正在使用我的浏览器时区,该时区与运行Docker容器的主机相同。 But debbuging the server side of the app I can see that the date is converted into UTC timezone. 但是调试应用程序的服务器端,我可以看到日期已转换为UTC时区。 This means that the docker container is not really using the host volume I did mount. 这意味着docker容器并没有真正使用我安装的主机卷。

Am I missing anything? 我有什么想念的吗?

Another way I tried and did work was updating the timezone in the docker container: 我尝试并完成工作的另一种方法是更新Docker容器中的时区:

$ dpkg-reconfigure tzdata // Selecting the corresponding options afterwards

This way I can see the same timezone in both: client side and server side of my app. 这样,我可以在应用程序的客户端和服务器端看到相同的时区。

Thanks in advanced 提前致谢

After debugging and reading about date and time, I think it makes sense that the backend stores the date and time in UTC/GMT, that way is independent of the client's timezone when it's saved in the DB. 调试并了解日期和时间后,我认为后端将日期和时间存储在UTC / GMT中是有道理的,这种方式与将客户端保存在数据库中时的时区无关。 So it wouldn't be a good practise to change the tomcat server timezone to match the host (it shouldn't really matter). 因此,更改tomcat服务器时区以匹配主机不是一个好习惯(这并不重要)。

The issue I had was the front end was using date and time (UTC/GMT +1) and the time was set to 00:00h and when it reaches the back end, the date and time is converted to UTC/GMT which makes it 23:00 of the previous day. 我遇到的问题是前端使用日期和时间(UTC / GMT +1),并且时间设置为00:00h,到达后端时,日期和时间被转换为UTC / GMT,因此前一天的23:00。 The persistence layer was just storing the date which it's wrong as we lose data (the time) and when we try to retrieve that record from the DB we will get the previous date without the time so it's not the result we would expect. 持久层只是存储错误数据的日期,因为我们丢失了数据(时间),当我们尝试从数据库中检索该记录时,我们将获得没有时间的前一个日期,因此这不是我们期望的结果。

I hope my explanation makes sense 我希望我的解释有意义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM