情况一、已经启动的container需修改时区

进入容器,输入date指令,可以看见当前容器内的系统时间

  1. root@148bce218932:/test# date
  2. Mon Jan 24 01:39:30 UTC 2022

不难发现,该时间与容器外的时间相差了8个小时,即一个时区,因此需要修改容器内的时区

通过tzselect更换时区

  1. root@148bce218932:/test# tzselect
  2. Please identify a location so that time zone rules can be set correctly.
  3. Please select a continent, ocean, "coord", or "TZ".
  4. 1) Africa
  5. 2) Americas
  6. 3) Antarctica
  7. 4) Asia
  8. 5) Atlantic Ocean
  9. 6) Australia
  10. 7) Europe
  11. 8) Indian Ocean
  12. 9) Pacific Ocean
  13. 10) coord - I want to use geographical coordinates.
  14. 11) TZ - I want to specify the time zone using the Posix TZ format.
  15. #? 4 //序号可能会存在些许差别,认准亚洲时区Asia
  16. Please select a country whose clocks agree with yours.
  17. 1) Afghanistan 18) Israel 35) Palestine
  18. 2) Armenia 19) Japan 36) Philippines
  19. 3) Azerbaijan 20) Jordan 37) Qatar
  20. 4) Bahrain 21) Kazakhstan 38) Russia
  21. 5) Bangladesh 22) Korea (North) 39) Saudi Arabia
  22. 6) Bhutan 23) Korea (South) 40) Singapore
  23. 7) Brunei 24) Kuwait 41) Sri Lanka
  24. 8) Cambodia 25) Kyrgyzstan 42) Syria
  25. 9) China 26) Laos 43) Taiwan
  26. 10) Cyprus 27) Lebanon 44) Tajikistan
  27. 11) East Timor 28) Macau 45) Thailand
  28. 12) Georgia 29) Malaysia 46) Turkmenistan
  29. 13) Hong Kong 30) Mongolia 47) United Arab Emirates
  30. 14) India 31) Myanmar (Burma) 48) Uzbekistan
  31. 15) Indonesia 32) Nepal 49) Vietnam
  32. 16) Iran 33) Oman 50) Yemen
  33. 17) Iraq 34) Pakistan
  34. #? 9 //中国
  35. Please select one of the following time zone regions.
  36. 1) Beijing Time
  37. 2) Xinjiang Time
  38. #? 1 //北京时间
  39. The following information has been given:
  40. China
  41. Beijing Time
  42. Therefore TZ='Asia/Shanghai' will be used.
  43. Local time is now: Mon Jan 24 09:50:14 CST 2022.
  44. Universal Time is now: Mon Jan 24 01:50:14 UTC 2022.
  45. Is the above information OK?
  46. 1) Yes
  47. 2) No
  48. #? 1
  49. You can make this change permanent for yourself by appending the line
  50. TZ='Asia/Shanghai'; export TZ
  51. to the file '.profile' in your home directory; then log out and log in again.
  52. Here is that TZ value again, this time on standard output so that you
  53. can use the /usr/bin/tzselect command in shell scripts:
  54. Asia/Shanghai

在底部加入TZ='Asia/Shanghai'; export TZ

  1. root@148bce218932:/test# vi /etc/profile

image.png

是配置文件全局生效并覆盖容器本地本地时间

  1. source /etc/profile
  2. cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

时间与外界一致

  1. root@148bce218932:/test# date
  2. Mon Jan 24 9:39:30 CST 2022

情况二、创建容器时指定启动参数,自动挂载localtime文件

  1. docker run -it -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime 127.0.0.1:5000/project:vtest

情况三、把时区设置加入到Dockerfile中

方法一

  1. # CentOS
  2. RUN echo "Asia/Shanghai" > /etc/timezone
  3. # Ubuntu
  4. RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

方法二

  1. # 添加时区环境变量,亚洲,上海
  2. ENV TimeZone=Asia/Shanghai
  3. # 使用软连接,并且将时区配置覆盖/etc/timezone
  4. RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone

相关文章

  1. docker容器中使用node-schedule