In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the time zone problem of how to deploy Django in the Docker container". In the daily operation, I believe that many people have doubts about how to deploy Django in the Docker container. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to deploy Django in the Docker container". Next, please follow the editor to study!
Catalogue
Time zone related configuration in Django
USE_TZ=True
USE_TZ=False
Setting of time zone in Linux container
Enter the Django environment to view time and time zone
Modify Linux container time zone
Enter Django environment to check the time
Summary
Now containerized deployment is very mature. Many of our services are deployed using containers, and it is very convenient to update and restore. However, there is a troublesome problem, that is, time zone processing, which is usually solved by injecting TZ environment variables, but in fact, this kind of processing is not possible in django.
Time zone related configuration in Django
In Django's configuration file settings.py, there are two configuration parameters related to time and time zone, which are TIME_ZONE and USE_TZ. We expected that Django would get the local time correctly by configuring it in settings.py, but in fact it backfired. Let's see what these two settings do.
USE_TZ=True
If USE_TZ is set to True, Django will use the default time zone of the system. At this time, the setting of TIME_ZONE is basically invalid, that is, it does not work with or without settings.
USE_TZ=False
If USE_TZ is set to False
TIME_ZONE is set to None
Django will still use the default time zone
If TIME_ZONE is set to another time zone
If it is a Windows system, the TIME_ZONE setting is useless and Django will use the native time
If it is another system, the UTC time of that time zone will be used
For example, set USE_TZ = False and TIME_ZONE = 'Asia/Shanghai', to use the UTC time in Shanghai.
At this point, you may think that the time is ready, but in fact, we still need to pay attention to the setting of the system time zone.
Setting of time zone in Linux container
Now my local time is: 16 TIME_ZONE 15 django is set to: USE_TZ = False, TIME_ZONE = 'Asia/Shanghai'
Do not inject TZ=Asia/Shanghai environment variables
Enter the container to check the container time and time zone
The system time shows the UTC time zone, which is exactly 8 hours short of 08:15.
Enter the Django environment to view time and time zone
Python manage.py shell from datetime import datetime datetime.now () # output datetime.datetime (2021, 10, 8, 8, 24, 8, 289230) from django.utils import timezone timezone.get_current_timezone_name () # output 'Asia/Shanghai'
Inject the environment variable TZ=Asia/Shanghai
Enter the container to check the time and time zone
The system time shows the Asia time zone, but the time is still the UTC time and does not show the real local time.
Enter the Django environment to view time and time zone
Python manage.py shell from datetime import datetime datetime.now () # output datetime.datetime (2021, 10, 8, 8, 24, 8, 289230) from django.utils import timezone timezone.get_current_timezone_name () # output 'Asia/Shanghai'
As you can see, although the time zone has changed, the time is still the UTC time, both in the container itself and in the Django
By inquiring on the Internet, we know that the time zone of the Linux system needs to be modified to modify the / etc/localtime file.
Modify Linux container time zone
The usual practice is to copy the host's / etc/localtime file to the container's / etc/localtime file, but we find through the query that the / etc/localtime file is really just a soft connection, and the actual file is: / usr/share/zoneinfo/Asia/Shanghai
Docker cp / usr/share/zoneinfo/Asia/Shanghai test:/etc/localtime
Without injecting TZ=Asia/Shanghai environment variables into the container, we log in to the container and find that the system time of the container has correctly obtained the local time and time zone
If the TZ=Asia/Shanghai environment variable is injected, even if the / etc/localtime file is replaced, only the time zone has changed and the time is still the UTC time
Enter Django environment to check the time
Python manage.py shell from datetime import datetime datetime.now () # output datetime.datetime (2021, 10, 8, 8, 43, 43, 754698)
The Linux system time is normal, but the time in the Django environment is still incorrect. It is still the UTC time. At this time, many people are a little crazy. They may think that there is something wrong with the USE_TZ and TIME_ZONE settings in settings.py, but that is not the problem. The reason is that the datetime library looks for the file Asia/Shanghai in the / usr/share/zoneinfo/ directory, which is not included in our image, so Django still uses the UTC time zone. The solution is very simple: create the / usr/share/zoneinfo/Asia directory and copy the files to this directory
# within the container (if this directory does not exist) mkdir-p / usr/share/zoneinfo/Asia # docker cp / usr/share/zoneinfo/Asia/Shanghai test:/usr/share/zoneinfo/Asia/Shanghai outside the container
Then log in to the container and check the time in the Django environment.
Python manage.py shell from datetime import datetime datetime.now () # output datetime.datetime (2021, 10, 8, 16, 49, 32, 57)
Now the time is absolutely right.
Summary
For the problem of container time zone, it is recommended to install and set / etc/localtime during the container production phase, for example, add the following statement to dockerfile
ADD / usr/share/zoneinfo/Asia/Shanghai / usr/share/zoneinfo/Asia/Shanghai RUN ln-s / usr/share/zoneinfo/Asia/Shanghai / etc/localtime
In this way, our container does not need to pay attention to the time zone when it is started. If the container has been made, mount the time zone file at startup.
Docker run-d-v / etc/localtime:/etc/localtime-v / usr/share/zoneinfo/Asia/Shanghai:/usr/share/zoneinfo/Asia/Shanghai imageName
This way is more troublesome. Another situation we encounter now is that the service is already online, and if you find that there is a problem with the time, copy the two files into the container manually, and then restart the container.
Docker cp / usr/share/zoneinfo/Asia/Shanghai test:/etc/localtime docker cp / usr/share/zoneinfo/Asia/Shanghai test:/usr/share/zoneinfo/Asia/Shanghai docker restart test at this point, the study on "how to deploy the time zone of Django in a Docker container" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 299
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.