docker timezone

Open
#19 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
docker

Research direction

Start by reviewing the repository's existing documentation or tip structure, then use the Docker commands and Dockerfile examples in this issue as the source material. Confirm the timezone guidance for Alpine, Debian, Ubuntu, and CentOS, and consider the work done when the complete Docker timezone explanation is integrated in the repository's expected format.

Written by the indexing model from the issue text.

Description

Docker 时区调整方案
https://cloud.tencent.com/developer/article/1626811

对于经常使用 Docker 的人来说,可能会碰到一个问题:时区问题。

大部分 Docker 镜像都是基于 Alpine,Ubuntu,Debian,CentOS 等基础镜像制作而成。

基本上都采用 UTC 时间,默认时区为零时区。

docker run --name test --rm -ti alpine /bin/sh
/ # date
Fri Nov 29 08:14:49 UTC 2019
复制
而我们主要用的是 CST 时间,北京时间,位于东八区。时区代号: Asia/Shanghai

docker run --name test --rm -ti -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro alpine /bin/sh
/ # date
Fri Nov 29 16:13:55 CST 201
复制
对比一下,我们会发现,时间上相差 8 小时。

经过一系列探索实践,我们总结了一些 Docker 时区调整方案。

一、运行 Docker 容器时调整时区
在 Linux 系统中,控制时区和时间的主要是两个地方:

/etc/timezone 主要代表当前时区设置,一般链接指向/usr/share/zoneinfo目录下的具体时区。
/etc/localtime 主要代表当前时区设置下的本地时间。

  1. 通用 docker 时区修改方案
    宿主机为 Linux 系统
    当宿主机为 Linux 系统时,我们可以直接将宿主机上的/etc/timezone和/etc/localtime挂载到容器中,这样可以保持容器和宿主机时区和时间一致。

-v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro
复制
使用示例如下:

docker run --name test --rm -ti -v /etc/timezone:/etc/timezone:ro -v /etc/localtime:/etc/localtime:ro alpine /bin/sh
/ # date
Fri Nov 29 16:13:55 CST 2019
复制
2. 通过传递环境变量改变容器时区
适用于基于 Debian 基础镜像, CentOS 基础镜像 制作的 Docker 镜像
不适用于基于 Alpine 基础镜像, Ubuntu 基础镜像 制作的 Docker 镜像
对于基于 Debian 基础镜像,CentOS 基础镜像制作的 Docker 镜像,在运行 Docker 容器时,传递环境变量-e TZ=Asia/Shanghai进去,能修改 docker 容器时区。

-e TZ=Asia/Shanghai
复制
使用示例如下:

docker run --name test -e TZ=Asia/Shanghai --rm -ti debian /bin/bash
/# date
Fri Nov 29 18:46:18 CST 2019
复制
二、制作 Docker 镜像时调整时区
通过编写 Dockerfile,构建自己的 Docker 镜像,可以永久解决时区问题。

  1. Alpine
    根据《Setting the timezone》提示,我们可以将以下代码添加到 Dockerfile 中:

ENV TZ Asia/Shanghai

RUN apk add tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime
&& echo ${TZ} > /etc/timezone
&& apk del tzdata
复制
2. Debian
Debian 基础镜像 中已经安装了 tzdata 包,我们可以将以下代码添加到 Dockerfile 中:

ENV TZ=Asia/Shanghai
DEBIAN_FRONTEND=noninteractive

RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime
&& echo ${TZ} > /etc/timezone
&& dpkg-reconfigure --frontend noninteractive tzdata
&& rm -rf /var/lib/apt/lists/*
复制
3. Ubuntu
Ubuntu 基础镜像中没有安装了 tzdata 包,因此我们需要先安装 tzdata 包。

我们可以将以下代码添加到 Dockerfile 中。

ENV TZ=Asia/Shanghai
DEBIAN_FRONTEND=noninteractive

RUN apt update
&& apt install -y tzdata
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime
&& echo ${TZ} > /etc/timezone
&& dpkg-reconfigure --frontend noninteractive tzdata
&& rm -rf /var/lib/apt/lists/*
复制
4. CentOS
CentOS 基础镜像 中已经安装了 tzdata 包,我们可以将以下代码添加到 Dockerfile 中。

ENV TZ Asia/Shanghai

RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime
&& echo ${TZ} > /etc/timezone
复制
总结
时区问题是大问题。

时间没统一好,业务会乱套。

希望通过上面的内容,能够帮助大家解决好 Docker 方面的时区问题。

Dominant language
Python
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from davideuler/programming-tips

All issues in davideuler/programming-tips

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.