Docker 运行中容器修改时区

很多国外的镜像默认时区不是中国上海,在运行后发现时区不对,这时候想要修改时区但又不能删除容器重新运行,如何处理?

参数说明

  • ${container-name}: 启动后的容器名称

进入容器bash

docker exec -t ${container-name} bash

设置时区

tzselect

根据选项提示选择亚洲-中国-上海,效果如下:

root@c7da5b2f6cde:/# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa							      7) Europe
2) Americas							      8) Indian Ocean
3) Antarctica							      9) Pacific Ocean
4) Asia								     10) coord - I want to use geographical coordinates.
5) Atlantic Ocean						     11) TZ - I want to specify the time zone using the Posix TZ format.
6) Australia
#? 4
Please select a country whose clocks agree with yours.
1) Afghanistan		   9) China		    17) Iraq		      25) Kyrgyzstan		33) Oman		  41) Sri Lanka		    49) Vietnam
2) Armenia		  10) Cyprus		    18) Israel		      26) Laos			34) Pakistan		  42) Syria		    50) Yemen
3) Azerbaijan		  11) East Timor	    19) Japan		      27) Lebanon		35) Palestine		  43) Taiwan
4) Bahrain		  12) Georgia		    20) Jordan		      28) Macau			36) Philippines		  44) Tajikistan
5) Bangladesh		  13) Hong Kong		    21) Kazakhstan	      29) Malaysia		37) Qatar		  45) Thailand
6) Bhutan		  14) India		    22) Korea (North)	      30) Mongolia		38) Russia		  46) Turkmenistan
7) Brunei		  15) Indonesia		    23) Korea (South)	      31) Myanmar (Burma)	39) Saudi Arabia	  47) United Arab Emirates
8) Cambodia		  16) Iran		    24) Kuwait		      32) Nepal			40) Singapore		  48) Uzbekistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1

The following information has been given:

	China
	Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Selected time is now:	Thu Dec 17 14:51:04 CST 2020.
Universal Time is now:	Thu Dec 17 06:51:04 UTC 2020.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
	TZ='Asia/Shanghai'; export TZ
to 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 you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

复制时区到localtime

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

退出容器

exec

重启容器

docker stop ${container-name}
docker start ${container-name}

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!

Flutter 极光推送对接指南 上一篇
Docker Redis容器间访问 下一篇