Is it possible change date in docker container?
- Aleksey Mitskevich
- 2015-04-10 08:35
- 4
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
4 Answers
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.
In 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*()
.
set time in docker container, How to change time in Docker container 1. Using date command The easiest way to change the time in a Docker container is to change the time using ‘date’ 2. Using environment variables The timezone of a container can be set using an environment variable in the docker 3. Using Dockerfile In How to change time in Docker container 1. Using date command The easiest way to change the time in a Docker container is to change the time using ‘date’ 2. Using environment variables The timezone of a container can be set using an environment variable in the docker 3. Using Dockerfile In
Aaron Digulla
2019-03-15 09:31
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!
docker change timezone in running container, 1. Log in to the container (attach it). # docker attach [container] 2. Backup this file inside the container. # mv /etc/localtime /etc/localtime.backup. 3. Create a soft link to the below file, please run this command. # ln -s /usr/share/zoneinfo/EST5EDT localtime. 1. Log in to the container (attach it). # docker attach [container] 2. Backup this file inside the container. # mv /etc/localtime /etc/localtime.backup. 3. Create a soft link to the below file, please run this command. # ln -s /usr/share/zoneinfo/EST5EDT localtime.
C4rlos96
2017-03-17 07:58
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 https://github.com/trajano/alpine-libfaketime and the docker image is in https://hub.docker.com/r/trajano/alpine-libfaketime
I also created a variant of it based on Ubuntu https://github.com/trajano/ubuntu-faketime
docker container use host timezone, The goal of this post is to set up the timezone on the container to match the same timezone on the Docker server, this will allow users to see the same time in both instances (Docker Server and Container). Most of the time Containers do not use the same time as the Docker server, this post will show how to configure this. The goal of this post is to set up the timezone on the container to match the same timezone on the Docker server, this will allow users to see the same time in both instances (Docker Server and Container). Most of the time Containers do not use the same time as the Docker server, this post will show how to configure this.
Archimedes Trajano
2019-04-20 12:51
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:
Remember to set the environment variables
LD_PRELOAD
before you run the application you want the faked time applied to.Example:
You can now dynamically change the servers time:
Example:
How to set system time dynamically in a Docker container, It is possible. The solution is to fake it in the container. This lib intercepts all system call programs use to retrieve the current time and date. The implementation is The easiest way to change the time in a Docker container is to change the time using 'date' command after connecting to the container. Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.
Vingtoft
2019-05-14 13:57