A method to solve the Docker container time zone and timing problem _docker

Source: Internet
Author: User
Tags local time

Today, a test bug was submitted by a tester during a system integration test because the time of submitting business data was biased against the actual time (Beijing time), resulting in statistical anomalies. Since our integration test is a test environment that provides the tester with a complete Docker mirror directly, the reason should be on the container time setting.

After you get the delivered Docker mirror, go to the container console after startup and use the date command to see if the time is correct. Re-view the host time is correct, this must be the container started without the time zone with the host to synchronize, because the test mirror is directly built by dockfile, so the problem is basic Dingzhun on the Dockerfile file.

When the Dockerfile check is turned on, it is found to be really missing the settings for the host time zone synchronization. So how to add a time zone synchronization process to record, at the same time to have encountered similar problems with students for reference:

The following is an example of a alpine-made Docker mirror (also our test image):

1 Dockerfile Modification

1.1 Increase Installation Tzdata pack

Install the Tzdata installation package when the package is installed and cannot clean the installation package after the build is successful

#定义环境变量
ENV time_zone asiz/shanghai
#dockerfile增加命令
RUN \

 #安装tzdata安装包

 && apk Add-- No-cache tzdata \

1.2 Increase the time zone configuration

When this installation package is installed, the time zone configuration information is generated in the/usr/share/zoneinfo directory, and the Alpine directory does not have timezone and locatime configuration. At this point we need to overwrite the time zone value into the timezone and localtime configuration of Alpine, as follows:

"
RUN
" ... #安装tzdata安装包
&& apk add--no-cache tzdata \ 
#设置时区
&& echo "${time_zone}" >/etc/timezone \ 
&& Ln-sf/usr/share/zoneinfo/${time_zone}/etc/localtime \

At this point, Dockerfile's revision work has been completed.

2 host time zone and Times check

Check the host for the correct time and time, using the following command:

' [Root@docker ~]# timedatectl local time:tue 2016-12-13 21:52:13 EST Universal time:wed 2016-12-14 02:52:13 UTC RTC time:wed 2016-12-14 02:52:13 time Zone:america/new_york (EST,-0500)//default to West five area NTP enabled:n/a NTP SYNCHR
         Onized:no RTC in-tz:no DST Active:no last DST Change:dst ended at Sun 2016-11-06 01:59:59 EDT Sun 2016-11-06 01:00:00 EST Next DST CHANGE:DST begins (the clock jumps one hour forward) at Sun 2017-03-12 0 1:59:59 EST Sun 2017-03-12 03:00:00 EDT #修改为东八区 [root@docker ~]# timedatectl set-timezone Asia/shanghai Local Ti me:wed 2016-12-14 10:53:10 CST Universal time:wed 2016-12-14 02:53:10 UTC RTC time:wed 2016-12-14 Tim E Zone:asia/shanghai (CST, +0800) NTP enabled:n/a NTP synchronized:no RTC in local tz:no DST active:n/a [root@d Ocker ~]# Date Wed Dec 10:53:49 CST 2016 #调整时间 [root@docker ~]# date-s "2016-12-13 21:54:20" #时间同步, cannot take effect without execution [root@dock Er ~]# clock-w [root@dOcker ~]# timedatectl Local time:tue 2016-12-13 22:59:44 CST Universal time:tue 2016-12-13 14:59:44 UTC RTC time : Tue 2016-12-13 14:59:44 time Zone:asia/shanghai (CST, +0800) NTP enabled:n/a NTP synchronized:no RTC in
 Tz:no DST active:n/a ' "

The host time zone and time have been adjusted to complete.

3. Build the container test

Regenerate the mirror based on the above dockerfile and start the container using the Run command to view the window time

/# date
Tue Dec 23:01:18 CST 2016

Synchronized with host, end of entire adjustment process ~ ~

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.