Several ways for the container to modify the time zone of the container

I. Introduction

2. Method 1: Use the method of mounting the time zone of the node node (tested ok)

apiVersion: v1
kind: Pod
metadata:
  name: xalpine-pod
  namespace: default
  labels:
    app: xalpine-pod
spec:
  containers:
  - name: xalpine-pod
    image: 37213690/xalpine:1.0
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
    imagePullPolicy: IfNotPresent
    volumeMounts:
      - name: host-time
        mountPath: /etc/localtime
        readOnly: true
  volumes:
    - name: host-time
      hostPath:
        path: /etc/localtime

3. Change the time zone when using alpine to create a mirror (tested ok)

FROM alpine:latest
MAINTAINER kahn "[email protected]"
# 包含nc、wget、curl、ssh等工具,tzdata是时区工具
ADD ./node_exporter /usr/bin/
RUN echo http://mirrors.aliyun.com/alpine/v3.18/main/ > /etc/apk/repositories && \
    echo http://mirrors.aliyun.com/alpine/v3.18/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade
WORKDIR /data
RUN apk --no-cache add ca-certificates wget curl openssh-client net-tools tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 9100
ENV girl="serena"
CMD ["/usr/bin/node_exporter"]
#CMD ["tail","-f"]

Note: use the following two lines

RUN apk --no-cache add  tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

4. Change the time zone when using busybox to make a mirror (not tested)

# 查看是否有Shanghai时区文件
ls /usr/share/zoneinfo/Asia/Shanghai
 
# 如果没有就需要获取时区文件,先进入busybox,如container_id=be318f78137f
docker exec -it xbusybox /bin/sh
mkdir -p /usr/share
exit
# 拷贝宿主机的时区文件到docker中
docker cp /usr/share/zoneinfo xbusybox:/usr/share/zoneinfo
 
# 进入busybox,同步时区
docker exec -ti xbusybox /bin/sh
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone

5. Change the time zone when using debian / ubuntu to create a mirror

# 进入容器命令行
docker exec -it [container_name | container_id] /bin/bash
 
# 列出安装的时区文件,验证是否存在tzdata。
ls /usr/share/zoneinfo
# 一般是已经安装了 timezone 数据包,如未安装则执行
apt-get install tzdata
 
# 软链接时区文件到localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

In addition, you can also add the following content to the dockerfile to complete the construction of the time zone:

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

6. Change the time zone when using centos to make a mirror image

# 进入容器命令行
docker exec -it [container_name | container_id] /bin/bash
 
# 一般都已经安装了 timezone 数据包,如遇到未安装则执行
yum install -y tzdata
 
# 软链接时区文件到localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

In dockerfile you can add:

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' >/etc/timezone

7. Expansion

If the docker image you obtained does not have root privileges, and you are in a hurry to enter the container to modify the time zone, then it is simple and violent, and directly copy the time zone file from the host machine to docker to realize the time zone modification:

docker cp /usr/share/zoneinfo/Asia/Shanghai 容器ID:/etc/localtime
echo 'Asia/Shanghai' >/etc/timezone && docker cp /etc/timezone 容器ID:/etc/timezone

Reference: https://blog.csdn.net/smooth00/article/details/107058753

Guess you like

Origin blog.csdn.net/xoofly/article/details/131653626