Solution for Docker container and host time out of sync

  

Many users have used Docker to reflect the time when the Docker container is out of sync with the host time. In fact, this is mainly because the host set the time zone, but the Docker container is not set, which causes them to be out of sync. The following small series will share with you the solution that the Docker container and the host time are out of sync.

Next, we execute the date command by the host and containers respectively under the point of view of the actual situation.

in the result of the host command execution date:

into the container, and then the result of executing date:

docker exec -it "containerid" /bin /sh

date

implementation of the results is:

from the screenshot, we found a difference of eight hours. So how to solve this problem? There are currently two ideas for reference.

Specify the startup parameters when creating the container, automatically mount the localtime file to 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

The above is the solution for the Docker container and host time out of sync. This problem is mainly due to the Docker container. There is no set time, so the user only needs to set it according to the method described above.

Copyright © Windows knowledge All Rights Reserved