The time of docker is not synchronized with the time of the host, and the configuration of docker-compose is done quickly! At the same time, it will take you to avoid the pits you may encounter

When using docker, it is found that some log time is wrong and the host machine is different for many days. This is absolutely impossible. The specific operation is as follows, use docker-compose to manage docker, then you can add the following line in the docker-compose.yml file:

services:
  myweb:
    image: centos
    volumes:
      - ".:/www/web"
#以下这行就是你要加入的配置,该行代码解释:分号前为你实际电脑时间文件的路径可能需要酌情修改建议使用实际路径如果填写软链接那么有可能会出现文末的报错(宿主机为mac,Linux等类Unix的系统,那么时间文件在/etc/localtime,不过这是软链接需要再看下这个链接实际指向哪里了);分号后面的照搬就好了;ro表示只读
      - "/private/var/db/timezone/tz/2019c.1.0/zoneinfo/Asia/Shanghai:/etc/localtime:ro"

Then re-docker-compose up and enter the container.At this time, output the date command, then the output time should be the same as your host.

If you start the container directly with the docker run command, then you only need to add the -v parameter after the docker run command. Here is an example:

docker run -v ".:/www/web" -v  "/private/var/db/timezone/tz/2019c.1.0/zoneinfo/Asia/Shanghai:/etc/localtime:ro"

Errors you may encounter: 

- docker-compose up时报错Cannot start service web: mkdir /private/var/db/timezone/zoneinfo: file exists

My solution to this problem is: When configuring volumes in docker-compose.yml before, the local machine filled in /etc/localtime. This path is a soft connection and I can restart it normally after I change it to the actual path.

 

Guess you like

Origin blog.csdn.net/weixin_37281289/article/details/106237084