Michaela Ervin
Michaela Ervin

Reputation: 750

linux "date" is correct but python tzlocal reports utc in docker container

I am mounting in the docker compose file

volumes:
  - /etc/localtime:/etc/localtime:ro

When I go into the container with docker exec -it <containername> bash then run date it shows up properly. But in django and in python3 editor tzlocal shows UTC.

ogixbuild$ docker exec -it qlogixwebapi bash root@3e1a15562c2f:/var/lib/django/webapi# date Thu Feb 8 15:14:31 MST 2018 root@3e1a15562c2f:/var/lib/django/webapi# python3 Python 3.6.4 (default, Dec 21 2017, 01:35:12) [GCC 4.9.2] on linux Type "help", "copyright", "credits" or "license" for more information. import tzlocal tzlocal.get_localzone() <StaticTzInfo 'Etc/UTC'>

Anyone have any experience with this know how to get tzlocal to get the correct timezone?

Upvotes: 1

Views: 486

Answers (1)

Michaela Ervin
Michaela Ervin

Reputation: 750

So by adding both volumes to the docker-compose file I now have working timezones. Both the date command and tzlocal work.

volumes:
  - /etc/localtime:/etc/localtime:ro
  - /etc/timezone:/etc/timezone:ro

root@96245334a7c9:/var/lib/django/webapi# date

Thu Feb 8 18:45:23 MST 2018

root@96245334a7c9:/var/lib/django/webapi# python3

Python 3.6.4 (default, Dec 21 2017, 01:35:12)

[GCC 4.9.2] on linux

Type "help", "copyright", "credits" or "license" for more information.

import tzlocal

tzlocal.get_localzone()

DstTzInfo 'America/Denver' LMT-1 day, 17:00:00 STD

As you can see both date and tzlocal now return the correct time with timezone. Now my django apps work as expected.

I like this approach because it gets the hosts time into the docker container dynamically. No hardcoded timezones or anything.

Upvotes: 1

Related Questions