Is it possible change date in docker container? Is it possible change date in docker container? docker docker

Is it possible change date in docker container?


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.gitWORKDIR /libfaketime/srcRUN 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    import datetime    print(datetime.datetime.today())    os.environ["FAKETIME"] = "2020-01-01"  #  string must be "YYYY-MM-DD hh:mm:ss" or "+15d"    print(datetime.today())


That's not possible with Docker. Docker uses the same clock as the outside kernel. What you need is full virtualization which emulates a complete PC.

The sudo fails because it only makes you root of the virtual environment inside of the container. This user is not related to the real root of the host system (except by name and UID) and it can't do what the real root could do.

If you use a high level language like Python or Java, you often have hooks where you can simulate a certain system time for tests or you can write code which wraps "get current time from system" and returns what your test requires.

Specifically for Java, use joda-time. There you can inject your own time source using DateTimeUtils.setCurrentMillis*().


This worked for me, maybe you could try it:

dpkg-reconfigure tzdata

Edit: Execute it inside the container you are having problems. An interface will appear. There you can edit the timezone and localtime for example, and set it correctly, that fixed my problem, that was the same as yours.

Good luck!