Ranjith Ramachandra
Ranjith Ramachandra

Reputation: 10764

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

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 following lines of code.

yum install -y ntp
service ntpd stop
ntpdate pool.ntp.org

What I would ideally like to do however is that the docker should sync time with the host. Is there a way to do this?

Upvotes: 140

Views: 303869

Answers (22)

Benyamin Jafari
Benyamin Jafari

Reputation: 34066

docker-compose form:

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

Upvotes: 14

Luvias
Luvias

Reputation: 570

This code worked for me

docker run -e TZ="$(cat /etc/timezone)" myimage

Upvotes: 0

Alexander Barker
Alexander Barker

Reputation: 321

This easy solution fixed our time sync issue for the docker kernel in WSL2.

Open up PowerShell in Windows and run this command to resync the clock.

wsl -d docker-desktop -e /sbin/hwclock -s

You can then test it using

docker run -it alpine date

Reference: https://github.com/docker/for-win/issues/10347#issuecomment-776580765

Upvotes: 19

mgarde
mgarde

Reputation: 693

I saw this on windows, launching Prometheus from docker-compose. I had a 15 hour time drift.

If you are running Docker Desktop on WSL, you can try running wsl --shutdown from a powershell prompt. Docker Desktop should restart, and you can try running your docker container again.

Worked for me, and I didn't have to restart.

Upvotes: 2

Goddard
Goddard

Reputation: 3059

For whatever reason none of these answers solved my problem.

It had nothing to do with the docker date/time for the images I was creating. It had to do with my local WSL date time.

Once I ran sudo ntpdate-debian everything worked.

If you don't have ntp just install it and run the command. If you aren't using debian then you probably won't have the shell script ntpdate-debian, but you can use ntpd -gq as well. Basically just update the date for your main WSL distro.

Upvotes: 0

Andreas Forslöw
Andreas Forslöw

Reputation: 2738

Windows users:

The solution is very simple. Simply open a powershell prompt and enter:

docker run --privileged --rm alpine date -s "$(Get-Date ([datetime]::UtcNow) -UFormat "+%Y-%m-%d %H:%M:%S")"

To check that it works, run the command:

docker run --rm -it alpine date


My solution is inspired by something I found in docker forum thread. Anyways, it was the only solution that worked for me on docker desktop, except for restarting my machine (which also works). Here's a link to the original thread: https://forums.docker.com/t/syncing-clock-with-host/10432/23

The difference between the thread answer and mine is that mine converts the time to UTC time, which is necessary for e.g. AWS. Otherwise, the original answer from the forum looks like this:

docker run --privileged --rm alpine date -s "$(date -u "+%Y-%m-%d %H:%M:%S")"

Upvotes: 8

Csongor Halmai
Csongor Halmai

Reputation: 3895

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

Upvotes: 2

Mindaugas B
Mindaugas B

Reputation: 1

Enabling Hyper-V in Windows Features solved the problem: Windows Features

Upvotes: 0

markhorrocks
markhorrocks

Reputation: 1518

Docker Usage

Here's a complete example which builds a docker image for a go app in a multistage build. It shows how to include the timezone in your image.

FROM golang:latest as builder

WORKDIR /app

ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o main

### Certs
FROM alpine:latest as locals

RUN apk --update --no-cache add ca-certificates

RUN apk add --no-cache tzdata

### App
FROM scratch 

WORKDIR /root/

COPY --from=locals /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

COPY --from=builder app/main .

COPY --from=builder app/templates ./templates

COPY --from=locals /usr/share/zoneinfo /usr/share/zoneinfo

ENV TZ=Asia/Singapore

EXPOSE 8000

CMD ["./main"]

Upvotes: 2

j3ffyang
j3ffyang

Reputation: 2470

I have the following in the compose file

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

Then all good in Gerrit docker with its replication_log set with correct timestamp.

Upvotes: 12

Y4roc
Y4roc

Reputation: 1136

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.

Upvotes: 74

Archmede
Archmede

Reputation: 1842

I've discovered that if your computer goes to sleep then the docker container goes out of sync.

https://forums.docker.com/t/time-in-container-is-out-of-sync/16566

I have made a post about it here Certificate always expires 5 days ago in Docker

Upvotes: 1

batristio
batristio

Reputation: 160

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.

Upvotes: 3

cahen
cahen

Reputation: 16656

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

Upvotes: 52

Ranjith Ramachandra
Ranjith Ramachandra

Reputation: 10764

The source for this answer is the comment to the answer at: Will docker container auto sync time with the host machine?

After looking at the answer, I realized that there is no way a clock drift will occur on the docker container. Docker uses the same clock as the host and the docker cannot change it. It means that doing an ntpdate inside the docker does not work.

The correct thing to do is to update the host time using ntpdate

As far as syncing timezones is concerned, -v /etc/localtime:/etc/localtime:ro works.

Upvotes: 139

Stefan
Stefan

Reputation: 9244

I was facing a time offset of -1hour and 4min

Restarting Docker itself fixed the issue for me.

To set the timezone in general:

  1. ssh into your container: docker exec -it my_website_name bash

  2. run dpkg-reconfigure tzdata

  3. run date

Upvotes: 11

cranehuang
cranehuang

Reputation: 505

For docker on macOS, you can use docker-time-sync-agent. It works for me.

Upvotes: 6

C12Z
C12Z

Reputation: 148

With docker for windows I had to tick

MobyLinuxVM > Settings > Integration Services > Time synchronization 

in Hyper-V manager and it worked

Upvotes: 5

lukecampbell
lukecampbell

Reputation: 15256

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.

Upvotes: 11

Travis Reeder
Travis Reeder

Reputation: 41123

It appears there can by time drift if you're using Docker Machine, as this response suggests: https://stackoverflow.com/a/26454059/105562 , due to VirtualBox.

Quick and easy fix is to just restart your VM:

docker-machine restart default

Upvotes: 9

gvlx
gvlx

Reputation: 688

If you are using boot2docker and ntp doesn't work inside the docker VM (you are behind a proxy which does not forward ntp packets) but your host is time-synced, you can run the following from your host:

docker-machine ssh default "sudo date -u $(date -u +%m%d%H%M%Y)"

This way you are sending your machine's current time (in UTC timezone) as a string to set the docker VM time using date (again in UTC timezone).

NOTE: in Windows, inside a bash shell (from the msys git), use:

docker-machine.exe ssh default "sudo date -u $(date -u +%m%d%H%M%Y)"

Upvotes: 40

Dharmit
Dharmit

Reputation: 5908

This is what worked for me with a Fedora 20 host. I ran a container using:

docker run -v /etc/localtime:/etc/localtime:ro -i -t mattdm/fedora /bin/bash

Initially /etc/localtime was a soft link to /usr/share/zoneinfo/Asia/Kolkata which Indian Standard Time. Executing date inside the container showed me same time as that on the host. I exited from the shell and stopped the container using docker stop <container-id>.

Next, I removed this file and made it link to /usr/share/zoneinfo/Singapore for testing purpose. Host time was set to Singapore time zone. And then did docker start <container-id>. Then accessed its shell again using nsenter and found that time was now set to Singapore time zone.

docker start <container-id>
docker inspect -f {{.State.Pid}} <container-id>
nsenter -m -u -i -n -p -t <PID> /bin/bash

So the key here is to use -v /etc/localtime:/etc/localtime:ro when you run the container first time. I found it on this link.

Hope it helps.

Upvotes: 31

Related Questions