To run a Docker container with the time being different of the real time.
A solution is to use libfaketime.
You can use LD_PRELOAD to load libfaketime in ahead of the normal system libraries so that it can intercept date/time system calls and returns a time that is adjusted by your preferred offset.
This can be done with containers or normal processes.
FROM debian:10.5 as builder RUN apt-get update && apt-get install -y make gcc git # Get the sources and checkout at stable release 0.98 # see https://github.com/wolfcw/libfaketime/releases RUN git clone https://github.com/wolfcw/libfaketime.git && \ cd libfaketime && \ git checkout ab1fc4eef31c7a64ce3a934588a1e67c41c8d256 && \ make FROM debian:10.5 as final COPY --from=builder /libfaketime/src/libfaketime.so.1 /usr/local/lib ENV LD_PRELOAD=/usr/local/lib/libfaketime.so.1 ENV FAKETIME="+2d"
Build it with:
sudo docker build --target final -t my/faketime .
NOTE: When a container is launched from this image it will appear to have a date/time exactly 2 days ahead of the real time as reflected by the host’s clock:
date Thu Aug 27 12:08:38 UTC 2020 sudo docker run --rm -it my/faketime date Thu Aug 29 12:08:45 UTC 2020
NOTE: A +2 day clock offset is baked into this image with an ENV directive.
You can also just specify FAKETIME when launching the container:
sudo docker run --rm -it --env FAKETIME=+9d my/faketime date Sat Sep 05 12:08:57 UTC 2020
NOTE: libfaketime** also supports other options like setting the absolute time rather than specifying an offset.