1

I am currently deploying openfaas on my local virtual machine's kubernetes cluster. I found that the time zone of the container started after publishing the function is inconsistent with the host machine. How should I solve this problem?

[root@k8s-node-1 ~]# date
# Host time
2021年 06月 09日 星期三 11:24:40 CST
[root@k8s-node-1 ~]# docker exec -it 5410c0b41f7a date
# Container time
Wed Jun  9 03:24:40 UTC 2021
silentao
  • 11
  • 1
  • I know nothing of OpenFaaS but I assume it's probably not doing anything special and the timezone inside the container is based on the `/etc/localtime` symlink like usual in Linux. Basically all container images will use UTC for that, most don't bother installing the tzdata files to save space. 11AM CST (UTC+8) is the same as 3AM UTC so that's entirely consistent. – coderanger Jun 09 '21 at 06:24
  • Yes,This question should be considered based on the container, nothing about openfaas, thanks for answering – silentao Jun 10 '21 at 05:02

1 Answers1

1

As @coderanger pointed out in the comments section, the timezone difference is not related to OpenFaaS.
It depends on the image you are using, most of the images use UTC timezone. Normally this shouldn't be a problem, but in some special cases you may want to change this timezone.

As described in this article, you can use the TZ environment variable to set the timezone of a container (there are also other ways to change the timezone).

If you have your own Dockerfile, you can use the ENV instruction to set this variable:
NOTE: The tzdata package has to be installed in the container for setting the TZ variable.

$ cat Dockerfile
FROM nginx:latest
RUN apt-get install -y tzdata
ENV TZ="Europe/Warsaw"

$ docker build -t mattjcontainerregistry/web-app-1 .
$ docker push mattjcontainerregistry/web-app-1
$ kubectl run time-test --image=mattjcontainerregistry/web-app-1
pod/time-test created

$ kubectl exec -it time-test -- bash
root@time-test:/# date
Wed Jun  9 17:22:03 CEST 2021
root@time-test:/# echo $TZ
Europe/Warsaw
matt_j
  • 4,010
  • 1
  • 9
  • 23