-v" command to share the local time of the host; 2. Use the "docker cp /etc/localtime:container ID/etc/localtime" command to copy the local time of the host."/> -v" command to share the local time of the host; 2. Use the "docker cp /etc/localtime:container ID/etc/localtime" command to copy the local time of the host.">
Home > Article > Operation and Maintenance > What should I do if the container time and host time in docker are not synchronized?
Method: 1. Use the "docker run -name
-v" command to share the local time of the host; 2. Use the "docker cp /etc/localtime:container ID/etc/localtime" command to copy the host's local time localtime.
The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.
The time zones of the two must be unified.
The localtime of the shared host (Method 1)
Specify the startup parameters when creating the container, mount the localtime file into the container, and ensure that the time zone used by both is Consistent.
docker run --name <name> -v /etc/localtime:/etc/localtime:ro ....
Copy the localtime of the host (Method 2)
docker cp /etc/localtime:【容器ID或者NAME】/etc/localtime
After completion, check the current time through the date command.
However, the time of the program running in the container may not be updated. For example, the MySQL service running in the container can be found by checking the MySQL time through sql after updating the time.
select now() from dual;
, the time has not changed.
At this time, you must restart the mysql service or restart the Docker container so that mysql can read the changed time.
Create a custom dockerfile (Method 3)
Create a dockerfile file. In fact, there is no content. It just customizes the time format and time zone of the image.
FROM redis FROM tomcat ENV CATALINA_HOME /usr/local/tomcat #设置时区 RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo 'Asia/Shanghai' >/etc/timezone \
After saving, use the docker build command to generate the image for use.
Recommended learning: "docker video tutorial"
The above is the detailed content of What should I do if the container time and host time in docker are not synchronized?. For more information, please follow other related articles on the PHP Chinese website!