How does openfaas solve the time zone problem of the container in the pod?

6/9/2021

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
kubernetes
openfaas
serverless

1 Answer

6/9/2021

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
Source: StackOverflow