Cloud Native 7 min read

Modify Docker Container Time Independently of the Host System

This guide explains how to adjust the clock inside a Docker container using libfaketime, avoiding host time changes, by leveraging privileged mode, capabilities, and environment variables, with step‑by‑step Dockerfile examples and log verification.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Modify Docker Container Time Independently of the Host System

Requirement

The client provides data with a time constraint and needs to test it at a specific timestamp without altering the host machine’s clock.

Host OS: CentOS Linux release 7.7.1908 (Core)

Implementation Logic

First, try to change the container’s 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 permitted
Fri Nov 25 15:48:51 UTC 2022

The date command fails because Docker isolates containers using Linux capabilities; modifying system time requires the SYS_TIME capability. Adding --cap-add, dropping capabilities with --cap-drop, or using --privileged can grant or restrict such permissions. The --privileged flag gives the container all capabilities.

Using --privileged to change time:

docker run --name ngx -d -p 80:80 --privileged nginx:alpine
docker exec -it ngx sh
# date
Fri Nov 25 15:57:09 UTC 2022
# date -s '2000-11-25 15:57:14'
Sat Nov 25 15:57:14 UTC 2000
# date -R
Sat, 25 Nov 2000 15:57:17 +0000

Time inside the container changes, and the host’s clock is also altered, which is undesirable when only the container should be affected.

Solution: use the open‑source libfaketime library to fake time inside a single container.

Installation Process (CentOS example)

Create a 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 80

Build the image: docker build -t time:centos ./ Run the container:

docker run --name centos -d -p 80:80 time:centos

Test 1 – Does Time Increment?

Access port 80 via a browser and view the container logs:

docker logs -f centos
192.168.199.10 - - [01/Jan/2000 01:00:00] "GET / HTTP/1.1" 200 -
... (repeated entries) ...

The timestamps remain fixed, showing that time does not advance when FAKETIME="2000-01-01 1:00:00" is used.

To make time progress, set FAKETIME="@2000-01-01 1:00:00" instead.

Update the 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 80

Rebuild and run:

docker build -t time:centos-v1 ./
docker run --name centos -d -p 80:80 time:centos-v1

Check logs again:

docker logs -f centos
192.168.199.10 - - [01/Jan/2000 01:00:10] "GET / HTTP/1.1" 200 -
192.168.199.10 - - [01/Jan/2000 01:00:20] "GET / HTTP/1.1" 200 -
192.168.199.10 - - [01/Jan/2000 01:00:22] "GET / HTTP/1.1" 200 -

The timestamps now increase, confirming the effect of the leading @ in FAKETIME .

Reference link: https://www.cnblogs.com/hukey/p/16926000.html (copyright belongs to the original author).

Diagram
Diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDockerfileLinux capabilitiescontainer timelibfaketime
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.