使用 docker 创建 django 应用的时候选择了体积相对较小的 alpine-python 镜像:
jfloff/alpine-python latest c0f8baca6066 7 days ago 238MB
然而测试的时候发现 django 设置中使用 Asia/Shanghai 时区并没有效果。
经研(gu)究(ge)发现 django 在设置环境的时候会从系统文件 zoneinfo 中读取时区:
if hasattr(time, 'tzset') and self.TIME_ZONE:
# When we can, attempt to validate the timezone. If we can't find
# this file, no check happens and it's harmless.
zoneinfo_root = '/usr/share/zoneinfo'
if (os.path.exists(zoneinfo_root) and not
os.path.exists(os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))):
raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE)
# Move the time zone info into os.environ. See ticket #2315 for why
# we don't do this unconditionally (breaks Windows).
os.environ['TZ'] = self.TIME_ZONE
time.tzset()
但是 alpine 镜像中并没有 /usr/share/zoneinfo 这个目录~
于是尝试创建此目录并拷贝 ubuntu 中对应的文件到 docker 对应的目录中 (Shanghai 这个文件是从 ubuntu 中拷出来的):
RUN mkdir -p /usr/share/zoneinfo/Asia/
RUN cp Shanghai /usr/share/zoneinfo/Asia/Shanghai
在 Dockfile 中加入上面的命令,重新 build 一个镜像,会发现 django 中的时区已经正常了~~
学习了
苦恼了几个小时,感谢老哥!
终于找到了老哥的新博客,想求一下Typecho的微语插件呢
放在这个博客上了,不过没有测试过跟新版的TE是否兼容,可以试试: https://qintianxiang.com/2019/06/typecho%e5%be%ae%e8%af%ad%e6%8f%92%e4%bb%b6/