The following is the solution to the time synchronization between the Docker container and the host. This problem is mainly caused by the Docker container not setting the time, so the user only needs to set it according to the method introduced below.
Next, we will see the actual situation by executing the date command in the host and container respectively.
The result of executing the date command on the host machine:
Enter the container and execute the date command. The result is:
docker exec -it "containerid" /bin/sh
date
The result after execution is:
Judging from the screenshots, we found that there is a difference of 8 hours. So how to solve this problem? There are currently two ideas for reference.
Specify startup parameters when creating a container and automatically mount localtime files into the container.
docker run --name "name" -v /etc/localtime:/etc/localtime:ro ...
Add time zone settings to Dockerfile
#Ubuntu
RUN echo “Asia/shanghai” 》 /etc/timezone;
#CentOS
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime