We often find that docker The time with the host is not synchronized , It's almost a pit , Especially the database system , Time error is fatal . This time is usually different 8 hour , Because our time is east eight district time , and docker It's standard time :

CST It means (China Shanghai Time, Eastern eight district time ) 
UTC It means (Coordinated Universal Time, standard time )

this 2 Time difference 8 Hours , Generally, the container has not been set , Time difference with host computer 8h, adopt date The order will tell . Although the question is simple , But if no one reminds you , A meal for another reason , Enough to drive a lot of people crazy ( I'm deploying docker Version of skywalking I made this mistake , How to modify the configuration can not see the monitoring data , Because all the data collected at present becomes 8 The historical data is hours ago ).

encounter docker Time zone inconsistency , We just need to synchronize it , But because docker The underlying operating system is different , Or the time zone tool is not installed in the system or not zoneinfo information , So our approach is slightly different :

1. Docker Common operating environment

docker Common operating systems include busybox,alpine,debian,ubuntu,centos, They're all different sizes , The scope of application will also vary , Generally by docker The project characteristics deployed in , Image size is certainly a priority :

Basically, except for busybox, Most of them linux The system can be ordered  cat /etc/issue To find out the version of the system :
# Enter the container command line docker exec -it [container_name | container_id] /bin/sh
########################################## / # cat /etc/issue Welcome to Alpine
Linux 3.12 Kernel \r on an \m (\l) ##########################################
root@9f1fc6293ff9:/# cat /etc/issue Debian GNU/Linux 10 \n \l
######################################### [root@qa ~]# cat /etc/issue CentOS
release 6.5 (Final) Kernel \r on an \m
about CentOS / Redhat It can be done through  cat /etc/redhat-release Look at the specific version :
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.7.1908
(Core)
  busybox and alpine System in bin Under the table of contents , You can find it busybox file :
/ # find /bin |grep busybox /bin/busybox

about docker Environmental Science , In addition to entering the container, use the above command view mode to determine the operating system version , Sometimes you can mirror a file dockerfile Of images Identification or RUN Command to judge ( It's not easy to judge , commonly apk The order is for alpine,apt-get It's aimed at debian or ubuntu,yum The order is for centos):

2. busybox Lower sync time zone

busybox It's an extremely lightweight version of the operating system , Most of the time zone data files cannot be installed , We can do it in a simple and crude way , Copy directly from host .
# See if there is one Shanghai Time zone file ls /usr/share/zoneinfo/Asia/Shanghai #
If not, you need to get the time zone file , Enter first busybox, as container_id=be318f78137f docker exec -ti
be318f78137f /bin/sh mkdir -p /usr/share exit # Copy the host's time zone file to docker in docker cp
/usr/share/zoneinfo be318f78137f:/usr/share/zoneinfo # get into busybox, Synchronize time zone docker
exec -ti be318f78137f /bin/sh ln -sf /usr/share/zoneinfo/Asia/Shanghai
/etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
After synchronization , adopt date Command to see that the time is synchronized with the host .

You can also use dockerfile To finish the work :
# It is necessary to /usr/share/zoneinfo Copy it first dockerfile Under the directory of COPY zoneinfo /usr/share/zoneinfo/
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo
'Asia/Shanghai' >/etc/timezone
3. alpine Lower sync time zone

alpine By default, there is no time zone file , It also needs to be installed :
# Enter the container command line docker exec -it [container_name | container_id] /bin/sh # install
timezone data packet , In order to prevent addition failure , add -U parameter , Update storage cache . apk add -U tzdata # Lists the installed time zone files , Verify that the download was successful . ls
/usr/share/zoneinfo # Copy the required time zone file to localtime, What we need at home is Asia/Shanghai: RUN ln -sf
/usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai'
>/etc/timezone # Verify time zone ,CST This is China standard time . date # Tue Jun 30 11:53:46 CST 2020 #
Remove time zone file : apk del tzdata
You can also use dockerfile Add the following to , Complete the construction of time zone :
# Install root filesystem ADD ./rootfs / # Install base packages RUN apk
update && apk add curl bash tree tzdata \ && cp -r -f
/usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo -ne "timezone
Asia/Shanghai. (`uname -rsv`)\n" >> /root/.built
4. debian / ubuntu Lower sync time zone
# Enter the container command line docker exec -it [container_name | container_id] /bin/bash #
Lists the installed time zone files , Verify existence tzdata. ls /usr/share/zoneinfo # It is usually installed timezone data packet , If not installed
apt-get install tzdata # Soft link time zone file to localtime ln -sf
/usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai'
>/etc/timezone
You can also use dockerfile Add the following to , Complete the construction of time zone :
ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &&
echo $TZ > /etc/timezone
5. centos Lower sync time zone
# Enter the container command line docker exec -it [container_name | container_id] /bin/bash # It's usually installed
timezone data packet , If not installed, execute yum install -y tzdata # Soft link time zone file to localtime ln -sf
/usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai'
>/etc/timezone
stay dockerfile You can add :
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo
'Asia/Shanghai' >/etc/timezone
If you get it docker There is no mirror image root jurisdiction , You're in a hurry to change the time zone , Just be violent , Copy the time zone file directly from the host to docker in , To modify the time zone :
docker cp /usr/share/zoneinfo/Asia/Shanghai container ID:/etc/localtime echo
'Asia/Shanghai' >/etc/timezone && docker cp /etc/timezone container ID:/etc/timezone

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network