情况一、已经启动的container需修改时区
进入容器,输入date指令,可以看见当前容器内的系统时间
root@148bce218932:/test# dateMon Jan 24 01:39:30 UTC 2022
不难发现,该时间与容器外的时间相差了8个小时,即一个时区,因此需要修改容器内的时区
通过tzselect更换时区
root@148bce218932:/test# tzselectPlease identify a location so that time zone rules can be set correctly.Please select a continent, ocean, "coord", or "TZ".1) Africa2) Americas3) Antarctica4) Asia5) Atlantic Ocean6) Australia7) Europe8) Indian Ocean9) Pacific Ocean10) coord - I want to use geographical coordinates.11) TZ - I want to specify the time zone using the Posix TZ format.#? 4 //序号可能会存在些许差别,认准亚洲时区AsiaPlease select a country whose clocks agree with yours.1) Afghanistan 18) Israel 35) Palestine2) Armenia 19) Japan 36) Philippines3) Azerbaijan 20) Jordan 37) Qatar4) Bahrain 21) Kazakhstan 38) Russia5) Bangladesh 22) Korea (North) 39) Saudi Arabia6) Bhutan 23) Korea (South) 40) Singapore7) Brunei 24) Kuwait 41) Sri Lanka8) Cambodia 25) Kyrgyzstan 42) Syria9) China 26) Laos 43) Taiwan10) Cyprus 27) Lebanon 44) Tajikistan11) East Timor 28) Macau 45) Thailand12) Georgia 29) Malaysia 46) Turkmenistan13) Hong Kong 30) Mongolia 47) United Arab Emirates14) India 31) Myanmar (Burma) 48) Uzbekistan15) Indonesia 32) Nepal 49) Vietnam16) Iran 33) Oman 50) Yemen17) Iraq 34) Pakistan#? 9 //中国Please select one of the following time zone regions.1) Beijing Time2) Xinjiang Time#? 1 //北京时间The following information has been given:ChinaBeijing TimeTherefore TZ='Asia/Shanghai' will be used.Local time is now: Mon Jan 24 09:50:14 CST 2022.Universal Time is now: Mon Jan 24 01:50:14 UTC 2022.Is the above information OK?1) Yes2) No#? 1You can make this change permanent for yourself by appending the lineTZ='Asia/Shanghai'; export TZto the file '.profile' in your home directory; then log out and log in again.Here is that TZ value again, this time on standard output so that youcan use the /usr/bin/tzselect command in shell scripts:Asia/Shanghai
在底部加入TZ='Asia/Shanghai'; export TZ
root@148bce218932:/test# vi /etc/profile

是配置文件全局生效并覆盖容器本地本地时间
source /etc/profilecp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
时间与外界一致
root@148bce218932:/test# dateMon Jan 24 9:39:30 CST 2022
情况二、创建容器时指定启动参数,自动挂载localtime文件
docker run -it -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime 127.0.0.1:5000/project:vtest
情况三、把时区设置加入到Dockerfile中
方法一
# CentOSRUN echo "Asia/Shanghai" > /etc/timezone# UbuntuRUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
方法二
# 添加时区环境变量,亚洲,上海ENV TimeZone=Asia/Shanghai# 使用软连接,并且将时区配置覆盖/etc/timezoneRUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
