Is it possible change date in docker container?

TomcatDocker

Tomcat Problem Overview


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

Tomcat Solutions


Solution 1 - Tomcat

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
    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())

Solution 2 - Tomcat

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*().

Solution 3 - Tomcat

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!

Solution 4 - Tomcat

I created a Docker image containing libfaketime for use with Alpine but the process can be done in other distributions.

Here's an example of using it Java using Groovy as an example. But Tomcat can be used as well.

FROM groovy:alpine
COPY --from=trajano/alpine-libfaketime  /faketime.so /lib/faketime.so
ENV LD_PRELOAD=/lib/faketime.so \
    DONT_FAKE_MONOTONIC=1

Then build and pass the FAKETIME environment variable when doing a docker run for example

docker build -f fakedemo-java.Dockerfile . -t fakedemo
docker run --rm -e FAKETIME=+15d fakedemo groovy -e "print new Date();"

Source is in trajano / alpine-libfaketime | Github and the docker image is in trajano/alpine-libfaketime | dockerhub

I also created a variant of it based on Ubuntu: trajano / ubuntu-faketime | Github

Solution 5 - Tomcat

For me, I actually needed to set the actual date for testing. I found the following options work on Mac, but you have to realize you'll be changing the date for all of your containers because you're changing the date of the underlying Alpine VM that Docker uses for all of its containers.

OPTION 1: Change the date of your host machine & restart docker

Use this when:

  • You can restart docker.
  • You can change the date of your host machine

Steps:

  1. Stop your containers.
  2. Change the date of your machine via the Date & Time Preferences
  3. Restart docker.
  4. Start your containers.

Run this sequence again to get back to the right date & time.

OPTION 2: Change the date of the Alpine VM

Use this when:

  • You can't restart docker.
  • You can't set the date of your host machine

Steps:

  1. screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
  • The screen starts blank, hit enter a few times.
  1. date -s [hh:mm]
  • All of your docker containers will now have your new time. You can also use other formats, look for documentation on “busybox date” as it’s not quite the same as other date implementations.
  1. To exit hit control-a : and type d
  • This detaches the screen session, but leaves the tty running.

To reset the time:

  1. screen -r
  • This resumes your tty.
  1. ntpd -q
  • This uses the server defined in /etc/ntp.conf (this looks like a magic bridge back to the host clock)
  1. To exit hit control-a : and type quit
  • This terminates your screen and tty session.

Solution 6 - Tomcat

docker exec -it [Container Id] /bin/bash (exec into container)

rm /etc/localtime (see time zone)

ln -s /usr/share/zoneinfo/Asia/Karachi /etc/localtime (set new time zone)

Solution 7 - Tomcat

I was having the same problem with my jenkins docker instance following steps fixed my problem

  1. exec into container

    docker exec -it 9d41c699a8f4 /bin/bash

  2. See time zone cat /etc/timezone : out put Etc/UTC

  3. set new time zone, with nano : Asia/Colombo (your timezone here)

  4. Restart the container

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAleksey MitskevichView Question on Stackoverflow
Solution 1 - TomcatVingtoftView Answer on Stackoverflow
Solution 2 - TomcatAaron DigullaView Answer on Stackoverflow
Solution 3 - TomcatC4rlos96View Answer on Stackoverflow
Solution 4 - TomcatArchimedes TrajanoView Answer on Stackoverflow
Solution 5 - TomcatJed AndersonView Answer on Stackoverflow
Solution 6 - TomcatImran RasheedView Answer on Stackoverflow
Solution 7 - TomcatSandOfTimeView Answer on Stackoverflow