skip to Main Content

Using macos Catalina and docker desktop.
The time of the conteiners perfectly syncs with the time in Vm Docker Desktop.

But I need to test one conteiner with date in the future.
I dont want to advance the clock of my mac because of iCloud services.

So I can achieve this just changing the hour in VM docker-desktop
I run:

docker run --privileged --rm alpine date -s "2023-02-19 11:27"

It changes the time ok. But it last just some seconds. Clearly there is some type of "syncronizer" that keeps changing back the time.

How do I disable this "syncronizer"?

2

Answers


  1. There’s only one time in Linux, it’s not namespaced, so when Docker runs ntp on the VM to keep it synchronized (in the past it would get out of sync, especially after the parent laptop was put to sleep), that sync applies to the Linux kernel, which applies to every container since it’s the same kernel value for everything. Therefore it’s impossible to set this on just one container in the Linux kernel.

    Instead, I’d recommend going with something like libfaketime that can be used to alter the response applications see when the query that time value. It basically sits as a layer between the kernel and application, and injects an offset based on an environment variable you set.

    FROM debian
    
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update 
     && apt-get install -y libfaketime 
     && rm -rf /var/lib/apt/lists*
    ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
    

    And then to run it, set FAKETIME:

    $ docker run --rm test-faketime date
    Thu Feb 17 14:59:48 UTC 2022
    
    $ docker run -e FAKETIME="+7d" --rm test-faketime date
    Thu Feb 24 14:59:55 UTC 2022
    
    $ date
    Thu 17 Feb 2022 09:59:57 AM EST
    
    Login or Signup to reply.
  2. I found that you can kill the NTP service which syncs the VM time to the host’s time. Details of how service works.

    First, use this guide to get a shell inside the VM.

    Then, find the sntpc service:

    / # ps a | grep sntpc
     1356 root      0:00 /usr/bin/containerd-shim-runc-v2 -namespace services.linuxkit -id sntpc -address /run/containerd/containerd.sock
     1425 root      0:00 /usr/sbin/sntpc -v -i 30 127.0.0.1
     3465 root      0:00 grep sntpc
    

    Take the number at the beginning of the /usr/sbin/sntpc line, and use kill to stop the process.

    / # kill 1425
    

    I have found that Docker Desktop does not seem to restart this process if it dies, and you can change the VM time without SNTPC changing it back.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search