如何更改 docker 容器中的时区? [英] How do I change timezone in a docker container?
问题描述
我正在为我从 docker-hub 提取的开发堆栈运行 docker 容器,该映像是为与我的应用程序应该部署的位置不同的时区创建的.
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.
如何在 docker 容器中更改时区?
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.
推荐答案
您可以在运行阶段按照@LinPy 的建议进行覆盖,但是如果您想在 Dockerfile 中进行设置,您可以使用 ENV 进行设置,因为 tzdata 已经存在你的基础图片.
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
构建
docker build -t dbtest .
运行
docker run -it dbtest -c "date"
现在您可以通过运行在数据库端进行验证
Now you can verify on DB side by running
显示时区;
您将在容器和 Postgres 中看到 Central Africa Time
You will see Central Africa Time
in both container and Postgres
在 alpine 基础镜像中,环境变量将不起作用.您将需要运行
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 &&
这篇关于如何更改 docker 容器中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!