How to change the time zone of your container
Generally speaking, in my experience most container images are using UTC, which seems like a reasonable default. It is consistent all around the world, and doesn’t change for daylight savings time, which helps to avoid ambiguous log entries… you should definitely consider using UTC.
However, either the image or the pod spec can be used to ensure that the time zone of the container is whatever you need it to be, if for some reason UTC is just not acceptable.
If you want to override the time zone in your image, in your Dockerfile you can specify something like the following:
FROM ubuntu:latestENV TZ="America/Toronto"
...
Depending on your base image (as is the case with the ubuntu:latest image above), you may need to install the tzdata package by adding something similar to the following:
RUN apt-get update && \
apt install -y --no-install-recommends tzdata && \
rm -rf /var/lib/apt/lists/* && \
apt-get clean
Note: Be aware that apt (and comparable package management technologies for other linux distributions such as yum, apk, dnf, etc.) will attempt to reach out to the image’s configured package repository when building the image. You may need to configure firewall rules for this to work, or you may need to reconfigure your image to point to a local mirror of the repository to comply with your organization’s best practices.
If you prefer to do this without making changes to the image, and assuming that the tzdata (or an equivalent) package is already present, you can simply add the environment variable in your Pod spec:
spec:
containers:
- name: my-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: TZ
value: "America/Toronto"
There are some alternative methods of handling this, depending on your use case:
- podman run has a
--tz
flag - podman also has lets you modify ~/.config/containers/containers.conf to add a tz setting on a new line in the file to be used as the default time zone
- docker run can mount timezone related files from the host, to ensure that time zone settings are consistant between the container and the host, e.g.
docker run -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime -it gcr.io/google-samples/node-hello:1.0
- You could use an admission controller (like Gatekeeper) to automatically add the TZ environment variable to all Pods if you want to automatically mandate the usage of a consistent time zone
- This is not recommended… but if you can create privileged Pods, you can do something similar in Kubernetes:
spec:
containers:
- name: my-container
image: gcr.io/google-samples/node-hello:1.0
volumeMounts:
- name: tz-config
mountPath: /etc/localtime
volumes:
- name: tz-config
hostPath:
path: /usr/share/zoneinfo/America/Toronto
type: File
This last approach seems to have little in the way of advantages — both in terms of impacts to security, but it also is more complex than the previous options — and as a result I would recommend that you try the other approaches first.
Hopefully this helps you configure the time zones of your containers!