1 min read

容器时区配置笔记

容器时区配置笔记

Alpine镜像

...
LABEL MAINTAINER="Qiu<qiu@xx.cn>"

RUN set -xe \
    && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk update \
    && apk add --no-cache ca-certificates tzdata \
    && cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
...

Debian镜像

FROM debian:10

ENV TZ="Asia/Shanghai"

Ubuntu镜像

RUN apt-get update && \
    apt-get install -yq tzdata && \
    ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata

ENV TZ="Asia/Shanghai"

示例:ubuntu-22.04

FROM ubuntu:22.04


ENV TZ Asia/Shanghai

RUN apt-get update \
   && apt-get install -yq tzdata locales \
   && rm -rf /var/lib/apt/lists/* \
  && ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
  && dpkg-reconfigure tzdata



CMD ["/bin/bash"]

YAML添加环境变量

      containers:
      - image: nginxinc/nginx-unprivileged:1.23.2
        name: nginx
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: Asia/Shanghai
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - -c
              - nginx -s quit; while killall -0 nginx; do sleep 1; done