45

Is there any way to set a Docker containers system time dynamically (at run time) without effecting the host machine?

Using

hwclock --set --date "Sat Aug 17 08:31:24 PDT 2016"

gives the following error:

hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.

Using

date -s "2 OCT 2006 18:00:00"

gives the following error:

date: cannot set date: Operation not permitted

Use case:

I need to test time sensitive software (behaviour depends on the date).

Other common use cases:

  • running legacy software with y2k bugs
  • testing software for year-2038 compliance
  • debugging time-related issues, such as expired SSL certificates
  • running software which ceases to run outside a certain timeframe
  • deterministic build processes.
4

3 Answers 3

34

It is possible

The solution is to fake it in the container. This lib intercepts all system call programs use to retrieve the current time and date.

The implementation is easy. Add functionality to your Dockerfile as appropriate:

WORKDIR /
RUN git clone https://github.com/wolfcw/libfaketime.git
WORKDIR /libfaketime/src
RUN make install

Remember to set the environment variables LD_PRELOAD before you run the application you want the faked time applied to.

Example:

CMD ["/bin/sh", "-c", "LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME_NO_CACHE=1 python /srv/intercept/manage.py runserver 0.0.0.0:3000]

You can now dynamically change the servers time:

Example:

import os
def set_time(request):
    print(datetime.today())
    os.environ["FAKETIME"] = "2020-01-01"  # Note: time of type string must be in the format "YYYY-MM-DD hh:mm:ss" or "+15d"
    print(datetime.today())
8
  • 8
    For others coming to this solution, be aware that this will not work for golang applications or other statically linked exes.
    – Sentinel
    Oct 16, 2018 at 11:38
  • 8
    Thanks @Sentinel. Funnily enough, I just found this answer via google, I specifically want to use it with a golang app, and I noticed that you left this comment on a 1.5-years-old question just 2 hours ago. Feels like it was for me. Thanks! :)
    – dimonomid
    Oct 16, 2018 at 13:42
  • Why does is not work in your situation? Does Golang not use system calls to retrieve time? @dimonomid I suggest you give it a try, it’s quick to implement the solution.
    – Vingtoft
    Oct 16, 2018 at 14:02
  • 1
    @Vingtoft Let me know if it does in fact work, but search around. Golang doesn't dynamically link in the libraries.
    – Sentinel
    Oct 16, 2018 at 16:06
  • 1
    @dimonomid Yeah I was pretty disappointed. More disappointed with Docker actually. I need to implement timing related tests. This makes Docker pretty much useless as a "container". I would have thought intercepting system time calls for timezone sync would have been item 1 on the Docker agenda. Apparently not.
    – Sentinel
    Oct 16, 2018 at 16:07
1

Here's docker-compose solution:

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

Check this link for getting an example.

1
  • This is exactly not what the question is asking for. You can't use this to dynamically change the time inside the container.
    – Herohtar
    Apr 28, 2022 at 20:05
-1

Start the container with an additional environment variable:

docker run -e "SET_CONTAINER_TIMEZONE=true" \
           -e "CONTAINER_TIMEZONE=US/Arizona" [docker image name]
1
  • Im looking for a way to set the time dynamically, meaning it can be set at runtime. I have updated my question.
    – Vingtoft
    Jan 6, 2017 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .