How do I change timezone in a docker container?

只谈情不闲聊 提交于 2021-02-18 05:24:06

问题


I am running docker container for my development stack which I pulled from docker-hub, the image is created for a different timezone than where my application is supposed to be deployed.

How do I change timezone in a docker container?

I tried to change the timezone config within the container by running

echo "Africa/Lusaka" > /etc/timezone

and restarted the container but I still get the same timezone.


回答1:


You can override as suggest by @LinPy during the run stage, but if you want to set at your Dockerfile you can set using ENV as tzdata is already there in your base image.

FROM postgres:10
ENV TZ="Africa/Lusaka"
RUN date

Build

docker build -t dbtest .

RUN

docker run -it dbtest -c "date"

Now you can verify on DB side by running

show timezone;

You will see Central Africa Time in both container and Postgres

in the alpine base image, the environment variable will not work. You will need to run

 RUN ls /usr/share/zoneinfo && \
cp /usr/share/zoneinfo/Europe/Brussels /etc/localtime && \
echo "Africa/Lusaka" >  /etc/timezone && \



回答2:


the best way is to use ENV in your run stage

-e TZ=Africa/Lusaka

and make sure that the package tzdata is present in the Container




回答3:


A simpler method would be to add an env var to your deployment:

env:
  - name: TZ
    value: "Europe/London"

(kubernetes deployment yaml)




回答4:


Simply change the /etc/localtime to the time zone in the /usr/share/zoneinfo directory.

follow these steps:

first log into bash of your container:

docker exec -u 0 -it mycontainer bash

then remove the symbolic link file (/etc/localtime):

sudo rm -rf /etc/localtime

Identify the timezone you want to configure and create the symbolic link for it:

For instance, I would like to set Asia/Tehran timezone:

ln -s /usr/share/zoneinfo/Asia/Tehran /etc/localtime

Now verify it by:

date

and the output would be your timezone:

Sat Jan 30 14:22:17 +0330 2021


来源:https://stackoverflow.com/questions/57607381/how-do-i-change-timezone-in-a-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!