Fake Time in Docker

What if you need a container to have a different time than its host? Well, LD_PRELOAD has a solution for you. While this solution generally works outside of Docker, I rarely find myself outside of a container these days. I like that it works in one without requiring special privileges.

Dockerfile:

FROM ubuntu:latest

RUN apt-get update --fix-missing
RUN DEBIAN_FRONTEND=noninteractive apt-get install git build-essential -y
RUN git clone https://github.com/wolfcw/libfaketime.git
WORKDIR /libfaketime/src
RUN make install
RUN echo "/usr/local/lib/faketime/libfaketime.so.1" > /etc/ld.so.preload

ENTRYPOINT bash

Build with:

docker build -t faketime .

Run with:

docker run -ti --rm -e FAKETIME="1970-01-01 00:00:00" --name faketime faketime

Of course you can run the “date” command to confirm, but this fake time percolates to all processes in the container. I was reminded of the existence of LD_PRELOAD to hijack system calls recently, and remembered this old trick I had stashed in my notes.

And just in case it goes away, I mirrored the excellent repo this is based on here.

Leave a Reply

Your email address will not be published. Required fields are marked *