Solution to the time synchronization between Docker container and host machine

Time: 2017-05-23Source: Huajun News NetworkAuthor: Ai Xiaomo

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.

Solution to the time synchronization between Docker container and host machine

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:

Solution to the time synchronization between Docker container and 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:

Solution to the time synchronization between Docker container and host machine

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

  

Related articles更多>>