How To Change The Time Zone For A Docker Container

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.

This is how this looks like before making the changes.

Container Time:

# docker exec 15c0c99b5c65 date
Wed Aug 23 15:40:24 UTC 2019

Docker Server Time:

# date
Wed Aug 23 11:40:26 EDT 2019

Chaning Docker Time Zone

In the example described in this post, the Docker server was on EDT time Zone and the Container was on UTC Time Zone. In case the Time Zone needs to be the same in both instances follow the below steps to complete this.

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

4. Finally, run the below command to make sure the time zone has been changed to EDT as same as the Docker Server.

# date

After these steps, the time should be the same in both instances.

Related Post