如何更改Docker容器中的时区? [英] How do I change timezone in a docker container?

查看:90
本文介绍了如何更改Docker容器中的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为从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中看到中部非洲时间

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 && \

这篇关于如何更改Docker容器中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆