18

Is it possible to change the system time in Docker containers without changing the host itself?

We are using Docker containers for testing which are sometimes time sensitive. I notice that any changes made to the host time are reflected to the Docker containers right away (which makes sense since they are using the same kernel). I am hoping, however, that Docker provides a way for you to somehow override the start time of the container and move from thereon.

Pang
  • 9,564
  • 146
  • 81
  • 122
user1717055
  • 241
  • 1
  • 2
  • 7
  • How are you tried to change system time in container? – Maxim Mar 11 '15 at 13:00
  • 2
    You are not allowed to update the datetime inside the container, you get Operation not permitted (probably because the container does not have enough privilege to mess around with the host kernel settings, etc). – user1717055 Mar 11 '15 at 14:13

3 Answers3

4

Just a follow up for those looking to have different datetime in Docker containers, https://github.com/wolfcw/libfaketime should probably be able to do the job.

Pang
  • 9,564
  • 146
  • 81
  • 122
user1717055
  • 241
  • 1
  • 2
  • 7
2

Simple:

Steps:

  1. First check the localtime of the docker container:

    [root@locdb oracle]# zdump /etc/localtime
    /etc/localtime  Tue May 22 13:59:40 2018 UTC
    
  2. Check the timezones like below:

    [root@locdb oracle]# zdump /usr/share/zoneinfo/* | tail -10
    /usr/share/zoneinfo/UTC          Tue May 22 14:01:21 2018 UTC
    /usr/share/zoneinfo/Universal    Tue May 22 14:01:21 2018 UTC
    /usr/share/zoneinfo/W-SU         Tue May 22 17:01:21 2018 MSK
    /usr/share/zoneinfo/WET          Tue May 22 15:01:21 2018 WEST
    /usr/share/zoneinfo/Zulu         Tue May 22 14:01:21 2018 UTC
    /usr/share/zoneinfo/iso3166.tab  Tue May 22 14:01:21 2018
    /usr/share/zoneinfo/posix        Tue May 22 14:01:21 2018
    /usr/share/zoneinfo/posixrules   Tue May 22 10:01:21 2018 EDT
    /usr/share/zoneinfo/right        Tue May 22 14:01:21 2018
    /usr/share/zoneinfo/zone.tab     Tue May 22 14:01:21 2018
    
  3. Copy the one that you need for example :

    cp  /usr/share/zoneinfo/UTC /etc/localtime
    
  • 7
    Note that this doesn't alter the time within the container (which is what the question asks for), it merely alters the timezone for the container. – phlummox Mar 30 '19 at 07:14
  • This answer addresses changing the *timezone*, not the *time*, e.g., this approach only works for very near-term time shifts. – Taylor D. Edmiston Mar 02 '22 at 17:05
0

As described in detail here the solution that I follow is to run docker on top of a VM and automate the whole process of changing-vm-time, running-vm and running-container, via vagrant.

Marinos An
  • 9,481
  • 6
  • 63
  • 96