我正在docker撰写文件中挂载
volumes:
- /etc/localtime:/etc/localtime:ro
当我带着
docker exec -it <containername> bash
然后运行date
,它会正确显示。但是在Django和python3编辑器中,tzlocal显示UTC。ogixbuild$ docker exec -it qlogixwebapi bash
root@3e1a15562c2f:/var/lib/django/webapi# date
Thu Feb 8 15:14:31 MST 2018
root@3e1a15562c2f:/var/lib/django/webapi# python3
Python 3.6.4 (default, Dec 21 2017, 01:35:12)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
import tzlocal
tzlocal.get_localzone()
<StaticTzInfo 'Etc/UTC'>
任何对此有任何经验的人都知道如何获取tzlocal以获得正确的时区?
最佳答案
因此,通过将两个卷都添加到docker-compose文件中,我现在有了工作时区。 date
命令和tzlocal均可使用。
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
root@96245334a7c9:/var/lib/django/webapi# date
Thu Feb 8 18:45:23 MST 2018
root@96245334a7c9:/var/lib/django/webapi# python3
Python 3.6.4 (default, Dec 21 2017, 01:35:12)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
import tzlocal
tzlocal.get_localzone()
DstTzInfo 'America/Denver' LMT-1 day, 17:00:00 STD
如您所见,
date
和tzlocal现在都返回带有时区的正确时间。现在,我的django应用程序可以按预期工作。我喜欢这种方法,因为它可以使主机时间动态地进入docker容器。没有硬编码的时区或其他任何内容。
关于django - linux “date”是正确的,但是python tzlocal报告docker容器中的utc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48695750/