Time in a Docker container

Viewed 5746

I have a couple of questions around time in a Docker container:

  1. Does a Docker container (e.g. ubuntu:16.04) have the same time as the host machine when it is started?
  2. Will the time be kept in sync, if I don't interfere?
  3. Will the time of the container be (1) kept in sync with the starting time or (2) kept in sync with the host or (3) be undefined or (4) something else, if I change the time on the host machine?
  4. If a CRON job within the continer should execute every full hour - is it guaranteed, that it will be executing?

What I tried

For (1), it looks as if it is the case ($ is host and # is container):

$ docker run -it ubuntu:18.04 bash
# date --iso-8601=s -u
2018-09-11T18:47:04+00:00
$ date --iso-8601=s -u
2018-09-11T18:47:10+00:00

For (3), I tried to change my local time with sudo date 080622432018, but I'm not sure if it took effect. I'm not sure if the command is wrong or if just some other system reset the time quickly to the correct one.

2 Answers

If you run following command on your linux host:

cat /sys/devices/system/clocksource/clocksource0/current_clocksource
tsc

You will see that your kernel is using (probably as mine) TSC what is Time Stamp Counter (https://en.wikipedia.org/wiki/Time_Stamp_Counter) - accurate time measurement based on CPU (here link to kernel parameter https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/kernel-parameters.txt#L523). As comparison when you issue that command inside VM based on KVM you will see there kvm-clock which helps to deal with problems related to time and full OS virtualization.

As docker container is light-weight virtualization isolation when you run same command in docker container you will see the same value - it means that container share time with host. It means also that container cannot change time without proper privileges because it will change time of host and all other containers - that privilege is SYS_TIME (https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities)

Answering your questions:

  1. yes
  2. yes
  3. will be the same as host
  4. Yes, at least there is high probability ;)

export your locales inside docker container

Related