Suppose you provide WordPress hosting using Docker containers to customers around the globe. Your customers would want to change time in Docker container to their time zones.
In our role as Server Support Specialists for web hosting companies and infrastructure providers, we provision and manage Docker systems for various business purposes.
Changing time in Docker container configuration is a task we perform as a part of this service. Here, we’ll see the different ways to do that.
How to change time in Docker container
The time in a Docker container can be changed in 5 ways. To know the current time, the ‘date’ command can be used.
docker exec -it container-id date
To know the timezone configured in a Docker container, the ‘ /etc/timezone ‘ file has to be checked.
docker exec -it container-id cat /etc/timezone
1. Using date command
The easiest way to change the time in a Docker container is to change the time using ‘date’ command after connecting to the container.
docker exec -it container-name /bin/bash date +%T -s "10:00:00"
Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.
2. Using environment variables
The timezone of a container can be set using an environment variable in the docker container when it is created.
docker run -e TZ=America/New_York ubuntu date
The time zone data package tzdata needs to be installed in the container for setting this timezone variable.
By configuring an NTP server, we ensure that the time zones in the containers are always synced.
3. Using Dockerfile
In hosting environment or cases which need too many identical containers to be spun up, the easiest way to manage is using Dockerfile.
The Dockerfile contains the basic configuration settings for each container. To change time in Docker container, the changes can be done in the corresponding Dockerfile.
The settings in the Dockerfile would reflect while recreating or adding a new container. And, all commands from the Docker file will be run as the root user. The entry in Dockerfile would look like:
RUN echo "Europe/Stockholm" > /etc/timezone RUN dpkg-reconfigure -f noninteractive tzdata
The installation command for tzdata package would vary based on the OS image that the container is using.
We also update the entry point script of the containers to include the timezone settings. This ensures that the date would be set whenever the container is restarted.
4. Using volumes
A major issue with Docker containers is that the data in the container is not persistent over restarts. To work around this, we use Docker data volumes.
Data volumes in Docker machines are shared directories that contains the data specific to containers. The data in volumes are persistent and will not be lost during container recreation.
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.
This time zone files of the host machine can be set in Docker volume and shared among the containers by configuring it in the Dockerfile as shown.
volumes: - "/etc/timezone:/etc/timezone:ro" - "/etc/localtime:/etc/localtime:ro"
The containers created out of this Dockerfile (docker-compose.yml) will have the same timezone as the host OS (as set in /etc/localtime file) .
5. Using images
Manually changing time zone is not feasible when there are too many containers. To create more Docker instances with the same time zone, we use images.
After setting the desired time zone in a Docker container, we exit it and create a new image from that container using ‘docker commit’. NTP service is also configured in that image.
docker commit container-name image-name
Using this image, we can create one or more containers with the same time zone. The images are stored in repositories for future use.
Conclusion
Docker containers are widely used in DevOps and niche web hosting. Today we’ve discussed the various ways how our Docker Support Specialists change time in Docker container in the Docker infrastructure we manage.
Your suggestions will not work in Windows environment. There is no such folder as “/etc/timezone” in Windows.
Using the above command line example to encode timezone in Ubuntu using the environment flag also failed. In Windows, it still return UTC. Etc.
So, for windows users, how would you modify your examples to get the local timezone to reflect on the containers?
One way I can think of is to manually create “/etc/timezone” folder in the host (aka windows) directory for the docker project and somehow “put in” the local timezone info in this folder. But how to do it? Any suggestions?
Hi Sorcerer,
We intended this article for Linux based Docker containers as a majority of our customers run Docker on *nix systems. You can use the same solution in Windows by installing cygwin (utility that allows Unix commands to be executed in Windows). You can use this command in a batch script (which we have not tested) to sync container time using Task Scheduler.
bash -c “docker run –net=host –ipc=host –uts=host –pid=host –security-opt=seccomp=unconfined –privileged –rm date -s “$(date -u ‘+%%Y-%%m-%%d %%H:%%M:%%S’)””
I’m a bit confused about the third solution.
“`Step 6/8 : RUN echo America/Vancouver > /etc/timezone
—> Using cache
—> 25762ca3d95f
Step 7/8 : RUN date && dpkg-reconfigure -f noninteractive tzdata
—> Running in f94c88c8310b
Sat May 4 00:32:23 UTC 2019
Current default time zone: ‘Etc/UTC’
Local time is now: Sat May 4 00:32:23 UTC 2019.
Universal Time is now: Sat May 4 00:32:23 UTC 2019.“`
Am I missing something? I tried using sudo in my dockerfile, but
`/bin/sh: 1: sudo: not found`
Hi Justin,
Commands in a Dockerfile run as root user. So you may have trouble while using ‘sudo’ in the command.
Still having problems, we can fix it for you. Talk to our sysadmins by clicking on the chat icon on right bottom of this page.
Commands in a Dockerfile run as root anyway so drop the sudo from the command
Yes. Corrected the commands. Thank you.
good post, it works correctly
hello .why is not work for me.
>docker exec -it 60b6b67e6a42 date +%z -s “0330”
date: cannot set date: Operation not permitted
+0000
Hi,
Please contact our support team via live chat
Impossible to use “date” for me (as root):
> date -s “23:55:00”
date: cannot set date: Operation not permitted
Hi,
Please contact our support team via live chat(click on the icon at right-bottom)