Time in a Docker container
Asked Answered
C

2

6

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.

Childbed answered 11/9, 2018 at 18:56 Comment(0)
R
6

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 ;)
Reel answered 11/9, 2018 at 21:8 Comment(0)
T
-4

export your locales inside docker container

Tasset answered 11/9, 2018 at 19:7 Comment(1)
Excuse me. 1. the time in a container take UTC longitude 0º 2. by example, if you deliver a web server inside the container, you need sincronice the server time with the local utc time. 2. if you install in the container a NTP client, this can does synchronized network time. 3. if you need fix the time, also you can do in a Dockerfile: example ENV LANG en_US.UTF-8 ENV LANGUAGE en_US:en ENV LC_ALL en_US.UTF-8 ENV TZ America/Bogota.Tasset

© 2022 - 2024 — McMap. All rights reserved.