Is there any way to set a Docker containers system time dynamically (at run time) without effecting the host machine?
Using
hwclock --set --date "Sat Aug 17 08:31:24 PDT 2016"
gives the following error:
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.
Using
date -s "2 OCT 2006 18:00:00"
gives the following error:
date: cannot set date: Operation not permitted
Use case:
I need to test time sensitive software (behaviour depends on the date).
Other common use cases:
- running legacy software with y2k bugs
- testing software for year-2038 compliance
- debugging time-related issues, such as expired SSL certificates
- running software which ceases to run outside a certain timeframe
- deterministic build processes.
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 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:
Here's
docker-compose
solution:Add
/etc/localtime:/etc/localtime:ro
to thevolumes
attribute.Check this link for getting an example.
Start the container with an additional environment variable: