是否有任何方法可以在不影响主机的情况下动态设置Docker容器系统时(在运行时)?
使用
hwclock --set --date "Sat Aug 17 08:31:24 PDT 2016"给出以下错误:
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.使用
date -s "2 OCT 2006 18:00:00"给出以下错误:
date: cannot set date: Operation not permitted我需要测试时间敏感的软件(行为取决于日期)。
其他常见用例:
发布于 2017-01-09 12:48:12
是可能的
解决办法是在容器里伪造它。这个库拦截用于检索当前时间和日期的所有系统调用程序。
实现起来很容易。酌情向Dockerfile添加功能:
WORKDIR /
RUN git clone https://github.com/wolfcw/libfaketime.git
WORKDIR /libfaketime/src
RUN make install请记住在运行应用程序之前设置环境变量LD_PRELOAD。
示例:
CMD ["/bin/sh", "-c", "LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME_NO_CACHE=1 python /srv/intercept/manage.py runserver 0.0.0.0:3000]现在您可以动态地更改服务器时间:
示例:
import os
def set_time(request):
print(datetime.today())
os.environ["FAKETIME"] = "2020-01-01" # Note: time of type string must be in the format "YYYY-MM-DD hh:mm:ss" or "+15d"
print(datetime.today())发布于 2018-08-21 05:53:40
发布于 2017-01-06 12:57:27
使用附加的环境变量启动容器:
docker run -e "SET_CONTAINER_TIMEZONE=true" \
-e "CONTAINER_TIMEZONE=US/Arizona" [docker image name]https://serverfault.com/questions/824631
复制相似问题