I'm running a script inside a Docker container (python:3.6) and it shows me the wrong time in the logs. When using the following python code in the python console(inside the container) it gives me the correct timezone.
import datetime
datetime.datetime.now()
datetime.datetime(2019, 8, 3, 17, 26, 25, 662809)
Also, when running the date
command inside the container, it gives the correct timezone.
However, I have this print statement in my script:
print("Updating....", datetime.datetime.now())
This however, gives me the wrong timezone (2 hours off).
This is my docker-compose.yml:
version: '3'
services:
app:
container_name: app
build: .
restart: unless-stopped
environment:
TZ: Europe/Amsterdam
depends_on:
- db
db:
container_name: db
image: mariadb
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: x
MYSQL_DATABASE: x
MYSQL_USER: x
MYSQL_PASSWORD: x
TZ: Europe/Amsterdam
volumes:
- /volumes/x/db:/var/lib/mysql
This is my crontab file (runs inside the app container)
CRON_TZ=Europe/Amsterdam
*/5 * * * * cd /usr/src/app && /usr/local/bin/python3.6 -u -m x > /proc/1/fd/1 2>/proc/1/fd/2
0,30 * * * * cd /usr/src/app && /usr/local/bin/python3.6 -u -m x > /proc/1/fd/1 2>/proc/1/fd/2
5 0 * * * cd /usr/src/app && /usr/local/bin/python3.6 -u -m x > /proc/1/fd/1 2>/proc/1/fd/2
I tried setting the timezone in docker-compose.yml
, that worked for date
command and the python console. I tried setting the timezone inside the crontab file, but it still doesn't show the correct timezone in the script.
How do I set the timezone for Python inside a crontab, so that the logs do have the correct timezone? Also, I need to run a script at 00:05 not 22:05, does that work now?
Setting the timezone for your Docker container You can set the timezone in your Docker image by adding a TZ environment variable to your configuration.
The -t (or --tty) flag tells Docker to allocate a virtual terminal session within the container. This is commonly used with the -i (or --interactive) option, which keeps STDIN open even if running in detached mode (more about that later).
If you are trying to sync the timezone in the container with the host machine, you can map the timezone setting from the host. I've done this successfully with the following volume:
-v /etc/localtime:/etc/localtime
Or in your docker-compose.yml file:
volumes:
- /etc/localtime:/etc/localtime
I got burned by this same problem a while back. Unfortunately, I don't remember the exact fix that ended up working. But here are some pointers:
1) The problem is with Docker, not Python or datetime or anything else. Docker containers struggle to know what the time is. What you want to search for is ways to sync up time inside a container with host.
2) There are many suggested ways of handling time in containers, but I remember them all being work-arounds of sorts. Last I checked there was no clear solution.
3) I would HIGHLY recommend that you do not put cron jobs inside containers. If you need things to run at a certain time, put the crons ON THE HOST, and have them spin up the containers when needed. This is much more reliable.
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