How to Change a Docker Container’s Clock Without Affecting the Host
To test data with strict time constraints without altering the host, this guide shows how to modify a Docker container’s clock using libfaketime, explains why direct date changes fail due to Linux capabilities, and provides step‑by‑step Dockerfile, build, run, and verification instructions.
Requirement
Customer data has a time limit; the test needs the data to be set to a specific point in time inside a Docker container without changing the host system time.
Implementation Logic
First, attempt to modify the container time directly.
docker run -d -p 80:80 nginx:alpine
docker exec -it d0d1cc9d5 sh
# date -R
Fri, 25 Nov 2022 15:48:40 +0000
# date -s '2000-11-25 15:48:51'
date: can't set date: Operation not permittedThe failure is caused by Linux capabilities: changing the system clock requires the SYS_TIME capability, which is not granted to containers by default. You can add capabilities with --cap-add or grant all privileges with --privileged.
Using --privileged allows the time change, but it also changes the host clock, which is undesirable when other containers share the same host.
Using libfaketime
The open‑source libfaketime library can fake the system time for a single container without affecting the host.
Installation (CentOS example)
FROM centos:7
RUN yum install make gcc gcc-c++ git -y && \
git clone https://github.com/wolfcw/libfaketime && \
cd libfaketime/src && \
make install
ENV LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 \
FAKETIME="2000-01-01 1:00:00"
ENTRYPOINT ["/usr/bin/python", "-m", "SimpleHTTPServer", "80"]
EXPOSE 80Build and run the image:
docker build -t time:centos ./
docker run --name centos -d -p 80:80 time:centosTest 1 – Does time increase?
Access the container’s port 80 and view the logs.
docker logs -f centos
192.168.199.10 - - [01/Jan/200001:00:00] "GET / HTTP/1.1" 200 -
... (repeated entries with the same timestamp)The timestamps are fixed because FAKETIME="2000-01-01 1:00:00" freezes time. To make time progress, use the @ prefix.
Update Dockerfile
FROM centos:7
RUN yum install make gcc gcc-c++ git -y && \
git clone https://github.com/wolfcw/libfaketime && \
cd libfaketime/src && \
make install
ENV LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 \
FAKETIME="@2000-01-01 1:00:00"
ENTRYPOINT ["/usr/bin/python", "-m", "SimpleHTTPServer", "80"]
EXPOSE 80Rebuild and run the updated image:
docker build -t time:centos-v1 ./
docker run --name centos -d -p 80:80 time:centos-v1
docker logs -f centos
192.168.199.10 - - [01/Jan/200001:00:10] "GET / HTTP/1.1" 200 -
192.168.199.10 - - [01/Jan/200001:00:20] "GET / HTTP/1.1" 200 -
192.168.199.10 - - [01/Jan/200001:00:22] "GET / HTTP/1.1" 200 -The logs now show timestamps advancing, confirming that the @ prefix makes the fake time increase while the host clock remains unchanged.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
