Skip to content

feat(docker): 可选注入 TZ 环境变量设置容器时区#1997

Open
maoqijie wants to merge 5 commits intoMCSManager:masterfrom
maoqijie:issue1087
Open

feat(docker): 可选注入 TZ 环境变量设置容器时区#1997
maoqijie wants to merge 5 commits intoMCSManager:masterfrom
maoqijie:issue1087

Conversation

@maoqijie
Copy link
Contributor

@maoqijie maoqijie commented Feb 1, 2026

背景/问题

在 Linux 上时区是“内核无关”的配置,Docker 仅共享宿主机内核,因此容器内时区需要在镜像构建或容器启动时单独设置。
目前 MCSManager 启动 Docker 容器时无法便捷地为容器注入 TZ 环境变量,导致部分程序/日志时间(CST/本地时间)不正确。

变更内容

  • 新增 Docker 实例配置:
    • enableTimezone: 是否启用容器时区注入(默认关闭)
    • timezone: 时区字符串(IANA 时区名,如 Asia/Shanghai / UTC
  • Daemon 在创建容器时:
    • enableTimezone=truetimezone 非空时,向容器 Env 注入 TZ=<timezone>
    • docker.env 中已有 TZ=...,会在启用注入时被覆盖为配置项的值(避免重复/冲突)
  • 前端实例 Docker 配置页新增:
    • “容器时区 (TZ)”开关 + 输入框(示例:Asia/Shanghai
    • 启用开关后若输入框为空,会尝试自动填充浏览器时区(Intl.DateTimeFormat().resolvedOptions().timeZone
    • 表单校验:启用时区注入时必须填写时区

使用方式

  1. 实例设置 → Docker → 开启「容器时区 (TZ)」
  2. 填写时区(例如 Asia/Shanghai
  3. 保存配置并重新启动该 Docker 实例

兼容性说明

  • 默认关闭,不影响现有用户/实例行为。
  • 未启用开关时,仍保持原来的 docker.env 行为(包括手动填写 TZ=... 也能继续使用)。
  • 注意:部分镜像若缺少 zoneinfo/tzdata,程序对 TZ 的支持可能有限;本 PR 仅负责注入环境变量,镜像侧依赖由用户自行保证。

测试/验证

  • 本地构建通过:
    • common / daemon / panel / frontend 均已构建成功
  • 语言包 JSON 校验通过

关联 Issue

Closes #1087

Added `enableTimezone` and `timezone` properties to the Docker configuration, allowing users to set the container's timezone. Updated relevant files to handle timezone injection into the container environment and added validation in the frontend. Translations for new messages have also been included in multiple languages.
@huangsijun17
Copy link
Contributor

容器,特别是Ubuntu容器,单独一个TZ变量可能不够。
Linux时区除了TZ变量,还有/etc/localtime(一般为软连接)和/etc/timezone(文本文件)例如Java就偏爱优先读取/etc/timezone作为时区。

Updated the Docker configuration to improve timezone handling by allowing the injection of `/etc/localtime` and `/etc/timezone` files when enabled. Added functions for sanitizing timezone names and checking mount targets. Updated relevant translations in multiple languages to reflect these changes.
@TillNano
Copy link

TillNano commented Feb 1, 2026

很多简化的环境容器并不自带有时区文件,建议添加/usr/share/zoneinfo的挂载,并且这些挂载尽量以只读模式(ro)进行。

Updated the Docker container setup to include the binding of `/usr/share/zoneinfo` alongside `/etc/localtime` and `/etc/timezone` when timezone support is enabled. This change ensures accurate timestamp handling for minimal Debian/Ubuntu images. Additionally, translations in multiple languages have been updated to reflect these enhancements.
@huangsijun17
Copy link
Contributor

huangsijun17 commented Feb 2, 2026

/etc/timezone 不推荐挂载。例如宿主机是红帽系,系统中就没有/etc/timezone。但容器基于Debian系,反而用了/etc/timezone。这样就会挂载失败。建议直接输出时区文本如Asia/Shanghai/etc/timezone

Enhanced the timezone injection process for Docker containers by ensuring the timezone text is written to `/etc/timezone` after container start, in addition to binding `/etc/localtime` and `/usr/share/zoneinfo`. Updated relevant translations in multiple languages to reflect these changes.
@HJWqa
Copy link
Contributor

HJWqa commented Feb 3, 2026

不需要修改所有语言文件,只保留中英文即可
而且就我经验,语言文件新增行放在倒数第二行而不是最后一行,可以有效解决冲突问题image
希望能帮到你

好吧,情况似乎比我想象的复杂image
至少这样子合并起来解决冲突更方便了,不是吗image💦

…uages

Removed redundant timezone-related entries and ensured consistency in the translation of the version detection error message across various language files. This update enhances the clarity and accuracy of the user interface in different locales.
@huangsijun17
Copy link
Contributor

从master往你的分支合并一下呢?

@HJWqa
Copy link
Contributor

HJWqa commented Feb 5, 2026

从master往你的分支合并一下呢?

试过,现在其实是这样的(如图是从这个分支往Master合并)
image
在VSCode 保留双方修改 或直接删除<<<< ==== >>>> 那些即可

还是不能一键合并,但是如果加在最后一行可能就是乱糟糟的感觉,合并起来难受一点
现在应该只需要等@yumao233 Resolve conflicts一下应该就行了
当然也可以本地从 masterissue1087 合并一下,看看效果,如果没有问题应该就不用再 git pushimage💦

docker相关PR可能会先被Merge进 Dev 分支进行统一测试

].filter(Boolean) as string[];

try {
this.container = await docker.createContainer(createContainerOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处功能上无问题,但是代码实现上并不是很好,多层 try catch 嵌套和多次重复 await docker.createContainer 不是很好,为了一个时区挂载功能感觉有点过了。

可以尝试将多次尝试抽离到多个函数中,从一个函数作为入口开始调用,如果是时区错误就进入到下一个函数,否则返回,实现代码可读性上的增强。

我能看出来几乎所有代码你都是通过 AI 自动生成,我作为人类静态的去阅读代码难以找出逻辑上的漏洞,估计你自己也不太清楚逻辑是否正确或有无 bug,这方面我希望你能录一个视频或多张截图来提供一个测试结果的保证。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

希望可以在Docker启动时,自动添加时区环境变量

5 participants