How to make sure docker's time syncs with that of the host?

前端 未结 17 1479
北恋
北恋 2020-12-02 07:10

I have dockers running on Linode servers. At times, I see that the time is not right on the dockers. Currently I have changed the run script in every docker to include the f

相关标签:
17条回答
  • 2020-12-02 07:21

    You can add your local files (/etc/timezone and /etc/localtime) as volume in your Docker container.

    Update your docker-compose.yml with the following lines.

    volumes:
        - "/etc/timezone:/etc/timezone:ro"
        - "/etc/localtime:/etc/localtime:ro"
    

    Now the container time is the same as on your host.

    0 讨论(0)
  • 2020-12-02 07:21

    If you're using docker-machine, the virtual machines can drift. To update the clock on the virtual machine without restarting run:

    docker-machine ssh <machine-name|default>
    sudo ntpclient -s -h pool.ntp.org
    

    This will update the clock on the virtual machine using NTP and then all the containers launched will have the correct date.

    0 讨论(0)
  • 2020-12-02 07:21

    For me, restarting Docker Desktop did not help. Shutting down Win10 and start it again, it did help.

    0 讨论(0)
  • 2020-12-02 07:22

    Although this is not a general solution for every host, someone may find it useful. If you know where you are based (UK for me) then look at tianon's answer here.

    FROM alpine:3.6
    RUN apk add --no-cache tzdata
    ENV TZ Europe/London
    

    This is what I added to my Dockerfile ^ and the timezone problem was fixed.

    0 讨论(0)
  • 2020-12-02 07:25

    This will reset the time in the docker server:

    docker run --rm --privileged alpine hwclock -s
    

    Next time you create a container the clock should be correct.

    Source: https://github.com/docker/for-mac/issues/2076#issuecomment-353749995

    0 讨论(0)
  • 2020-12-02 07:25

    docker-compose usage:

    Add /etc/localtime:/etc/localtime:ro to the volumes attribute:

    version: '3'
    
    services:
      a-service:
          image: service-name
          container_name: container-name
          volumes:
            - /etc/localtime:/etc/localtime:ro
    
    0 讨论(0)
提交回复
热议问题