Set timezone in asp.net 6.0 in linux container
- 1 minutes read - 189 wordsAccording to many articles one the web, the way to set timezone is as following:
cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime
echo 'Asia/Singapore' > /etc/timezone
You can indeed get the correct time if you run it in dotnet core 5 docker linux containers. However you couldn’t get the correct local time in dotnet 6.0. I found the issue About time incorrect of docker image about aspnet-6 #62545. After I tried serveral combinations mentioned in the issue, finally I got a working solution. Here is my solution.
cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime
echo 'Asia/Singapore' > /etc/timezone
export TZ=Asia/Singapore
In my k8s environemts, I used kustomize to manage yaml resources. To minimize this change, I use a jsonpatch to patch all my relevant deployemnts. Here is my jsonpatch and a fraction of kustomization.yaml.
// filename: tz-patch.json
[
{
"op": "add",
"path": "/spec/template/spec/containers/0/lifecycle",
"value": {
"postStart": {
"exec": {
"command": [
"/bin/sh",
"-c",
"cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime && echo 'Asia/Singapore' > /etc/timezone"
]
}
}
}
},
{
"op": "add",
"path": "/spec/template/spec/containers/0/env/0",
"value": {
"name": "TZ",
"value": "Asia/Singapore"
}
}
]
# filename: kustomization.yaml
patches:
- path: tz-patch.json
target:
group: apps
kind: Deployment
name: app.* #regexp
version: v1