HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

Incorrect timezoneID inside docker container

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
incorrectcontainerdockertimezoneidinside

Problem

I have created a docker image from the base Centos7 image and while running the container from it, the time and timezone were not in sync with my host. To give the container the same timezone as that of the host. I mounted /etc/timezone and /etc/localtime from the host to the container using the '-v' argument while running the container. After running the container with this, this is what i get inside the container and host:

For docker container running timedatectl:


[root@d7a24c723f6a /]# timedatectl Local time: Tue 2017-08-01 10:28:30
IST Universal time: Tue 2017-08-01 04:58:30 UTC RTC time: n/a Time
zone: UTC (IST, +0530) NTP enabled: n/a NTP synchronized: no RTC in
local TZ: no

For host running timedatectl:


[root@indiadev72 ~]# timedatectl Local time: Tue 2017-08-01 10:27:29
IST Universal time: Tue 2017-08-01 04:57:29 UTC RTC time: Tue
2017-08-01 04:57:29 Time zone: Asia/Kolkata (IST, +0530) NTP enabled:
yes NTP synchronized: no RTC in local TZ: no DST active: n/a

I am not sure why my docker container is not able to take the timezone of the host.

EDIT: Also while running some of my test cases, i am able to see that the application is reading timezoneid as 'GMT+530' instead of Asia/Kolkata. While GMT+530 is indeed Asia/Kolkata but due the timezone ID mismatch inside the container my test cases are failing. Maybe its just the timezoneID issue and the container is picking up the correct time. Can someone please help here?

Solution

The case is dpkg-reconfigure tzdata simply creates /etc/localtime as a copy, hardlink or symlink (a symlink is preferred) to a file in /usr/share/zoneinfo. So it is possible to do this entirely from your Dockerfile. Consider:

ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone


And as a bonus, TZ will be set correctly in the container as well.

This is also distribution-agnostic, so it works with pretty much anything Linux.

The original answer: https://serverfault.com/questions/683605/docker-container-time-timezone-will-not-reflect-changes

Code Snippets

ENV TZ=Asia/Kolkata
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Context

StackExchange DevOps Q#1640, answer score: 1

Revisions (0)

No revisions yet.