How to change the time for a Docker container from the outside

Posted . Visible to the public.

I had to modify the time for an application that I launch through Docker.
Here is an approach that worked for me without modifying or wrapping the container image.

Note

This is quite hacky and may not work for everyone. The "proper" way is probably to have a custom Dockerfile to wrap an existing image, or adjust a custom image's Dockerfile. I wanted to do neither.

So, I had an application that I ran through a pre-built image. Basically this:

docker run --rm -it example/image

The faketime Show archive.org snapshot tool allows modifying the time of a Linux process, like so:

$ faketime 'last Friday 4 pm' date
Fr 12. Sep 16:00:00 CEST 2025

However, we cannot just modify the time of the docker process. We need to affect the command being run inside the image.

So we need to smuggle faketime into the container somehow, and activate it for the process being run.
The first can be achieved by mounting faketime's libraries via --volume, the latter through the LD_PRELOAD Show archive.org snapshot environment variable.

  1. If you don't have faketime installed on your host system, install it.
  2. Find out the directory for its library files, e.g. with find /usr/lib -type d -name faketime. We'll mount that into the container.
  3. Run the container with env variables LD_PRELOAD and FAKETIME with the desired time.

Here is the resulting command:

docker run --rm -it \
  --env FAKETIME="@2025-09-18 12:00:00" \
  --env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 \
  --volume /usr/lib/x86_64-linux-gnu/faketime:/usr/lib/x86_64-linux-gnu/faketime:ro \
  example/image

Note that faketime does not "freeze" time, just sets the start time. Time will progress as usual.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2025-09-18 12:03)