Timezone synchronization between different Docker operating systems

We often find that the time of the docker and the host is out of sync, which is almost a pit, especially in the database system, the time error is simply fatal. This time is generally 8 hours apart, because our time is the East Eight District time, and docker uses the standard time:

CST refers to (China Shanghai Time, East Eighth District Time) 
UTC refers to (Coordinated Universal Time, Standard Time)

The difference between the two times is 8 hours. Generally, the time difference between the container that has not been set and the host time is 8 hours, which can be seen by the date command. Although this problem is very simple, if no one reminds me, looking for other reasons is enough to make many people crazy (I made this mistake when deploying the docker version of skywalking, and I can't see the monitoring data no matter how I modify the configuration. , because the currently collected data has become the historical data 8 hours ago).

When the time zone of docker is inconsistent, we only need to synchronize it. However, because the basic operating system running on docker is different, or the time zone tool is not installed in the system or there is no zoneinfo information, our processing method is slightly different. :

1. Docker commonly used operating environment

The operating systems commonly used by docker include busybox, alpine, debian, ubuntu, and centos. Their sizes are different, and the scope of application will also be different. Generally, it is determined by the characteristics of the project deployed in docker. The size of the image must also be a priority factor. :

Basically, except busybox, most linux systems can get their system version through the command cat /etc/issue:

# 进入容器命令行
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

For CentOS / Redhat, you can view the specific version by cat /etc/redhat-release:

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)

  The busybox and alpine systems are in the bin directory, and the busybox file can be found:

/ # find /bin |grep busybox
/bin/busybox

For the docker environment, in addition to entering the container and using the above command to view the operating system version, sometimes it can be judged by the images logo of the image file dockerfile or the RUN command (in fact, it is not easy to judge, the general apk command is for alpine, apt- get is for debian or ubuntu, yum command is for centos):

2. Time zone synchronization under busybox

busybox is an extremely lightweight version of the operating system. In many cases, time zone data files cannot be installed. We can use a simple and rude way to copy directly from the host machine.

# 查看是否有Shanghai时区文件
ls /usr/share/zoneinfo/Asia/Shanghai

# 如果没有就需要获取时区文件,先进入busybox,如container_id=be318f78137f
docker exec -ti be318f78137f /bin/sh
mkdir -p /usr/share
exit
# 拷贝宿主机的时区文件到docker中
docker cp /usr/share/zoneinfo be318f78137f:/usr/share/zoneinfo

# 进入busybox,同步时区
docker exec -ti be318f78137f /bin/sh
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

After synchronizing the time, you can see that the time has been synchronized with the host through the date command.

Alternatively, this can be done in a dockerfile:

# 需将/usr/share/zoneinfo先拷到dockerfile的目录下
COPY zoneinfo /usr/share/zoneinfo/
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

3. Synchronize time zone under alpine

Alpine also does not have a time zone file by default, and also needs to be installed:

# 进入容器命令行
docker exec -it [container_name | container_id] /bin/sh

# 安装 timezone 数据包,为了防止添加失败,加上-U参数,更新仓储缓存。
apk add -U tzdata

# 列出安装的时区文件,验证是否下载成功。
ls /usr/share/zoneinfo

# 拷贝需要的时区文件到localtime,国内需要的是Asia/Shanghai:
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

# 验证时区,CST 即为中国标准时间。
date
# Tue Jun 30 11:53:46 CST 2020

# 移除时区文件:
apk del tzdata

In addition, you can also add the following content to the dockerfile to complete the construction of the 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. Sync time zone under debian/ubuntu

# 进入容器命令行
docker exec -it [container_name | container_id] /bin/bash

# 列出安装的时区文件,验证是否存在tzdata。
ls /usr/share/zoneinfo
# 一般是已经安装了 timezone 数据包,如未安装则执行
apt-get install tzdata

# 软链接时区文件到localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

In addition, you can also add the following content to the dockerfile to complete the construction of the time zone:

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

5. Sync time zone under centos

# 进入容器命令行
docker exec -it [container_name | container_id] /bin/bash

# 一般都已经安装了 timezone 数据包,如遇到未安装则执行
yum install -y tzdata

# 软链接时区文件到localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

In dockerfile you can add:

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' >/etc/timezone

If the docker image you obtained does not have root permissions, and you are anxious to enter the container to modify the time zone immediately, then it is simple and violent to copy the time zone file directly from the host to the docker to realize the modification of the time zone:

docker cp /usr/share/zoneinfo/Asia/Shanghai 容器ID:/etc/localtime
echo 'Asia/Shanghai' >/etc/timezone && docker cp /etc/timezone 容器ID:/etc/timezone

Guess you like

Origin blog.csdn.net/smooth00/article/details/107058753