在Docker容器中是否可能更改日期? [英] Is it possible change date in docker container?
问题描述
可以在码头工作吗?我读到,如果我在容器中更改日期,日期将在主机系统上更改。但对我来说这是一个坏主意。我需要在一个服务器上提供这个应用程序的一些实例,并且可以为每个实例设置不同的时间。
但是当我尝试更改容器内的日期时我收到错误:
sudo date 04101812
日期:无法设置日期:不允许操作
Fri 4月10日18:12:00 UTC 2015
非常有可能动态地更改Docker容器中的时间,而不会影响主机操作系统。
解决方法是伪造它。 此lib 拦截用于检索当前时间和日期的所有系统调用程序。
实现很简单。根据需要向Dockerfile添加功能:
WORKDIR /
运行git clone https://github.com/wolfcw /libfaketime.git
WORKDIR / libfaketime / src
运行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]
您现在可以动态更改服务器的时间:
示例:
def set_time(request):
import os
print(datetime.today())
os。 environ [FAKETIME] =2020-01-01#注意:字符串类型的时间必须格式为YYYY-MM-DD hh:mm:ss或+ 15d
print(datetime .today())
I have a container with a running program inside tomcat. I need to change date only in this container and test my program behaviour. I have time sensitive logic, and sometimes need to see what happens in a few days or months later. Is it possible in docker? I read that if I change date in container, date will get changed on the host system. But it is a bad idea for me. I need to have a few instances of this application on one server and have possibilities of setting up different time for each instance.
But when I try to change date inside the container I get the error:
sudo date 04101812
date: cannot set date: Operation not permitted
Fri Apr 10 18:12:00 UTC 2015
It is very much possible to dynamically change the time in a Docker container, without effecting the host OS.
The solution is to fake it. This lib intercepts all system call programs use to retrieve the current time and date.
The implementation is easy. Add functionality to your Dockerfile as appropriate:
WORKDIR /
RUN git clone https://github.com/wolfcw/libfaketime.git
WORKDIR /libfaketime/src
RUN make install
Remember to set the environment variables LD_PRELOAD
before you run the application you want the faked time applied to.
Example:
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]
You can now dynamically change the servers time:
Example:
def set_time(request):
import os
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())
这篇关于在Docker容器中是否可能更改日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!