Is it possible to change the system time in Docker containers without changing the host itself?
We are using Docker containers for testing which are sometimes time sensitive. I notice that any changes made to the host time are reflected to the Docker containers right away (which makes sense since they are using the same kernel). I am hoping, however, that Docker provides a way for you to somehow override the start time of the container and move from thereon.
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.
The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time. The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).
Docker images are a little bit different from a virtual machine snapshot, though. For starters, Docker images can't ever change. Once you've made one, you can delete it, but you can't modify it. If you need a new version of the snapshot, you create an entirely new image.
Docker containers don't inherit the host's timezone, so you can run into unexpected scheduling issues that wreak havoc with your application. The container is using the UTC timezone, creating a one hour difference between the two times.
Just a follow up for those looking to have different datetime in Docker containers, https://github.com/wolfcw/libfaketime should probably be able to do the job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With