首页 > 解决方案 > Docker 映像中的日期、时间和时区错误。无法设置

问题描述

以下是我的 dockerFile。

FROM python:3.8

LABEL version="0.1"
WORKDIR /app
COPY . /app

RUN  pip install -r requirements.txt
CMD ["sh", "-c", "python3 run_scheduler.py "]

我确实设法更改了 time_zone,但日期和时间仍然是错误的。它显示日期时间为 020-06-11 12:10:37.595709, tz:Etc/UT。我需要 tz 作为 America\New_York。

标签: pythondocker

解决方案


You can set the respective timezone by creating a link of /etc/timezone and /etc/localtime to point to necessary timezone file file. In your case, point the link to /usr/share/zoneinfo/America/New_York.

You can create the link as a part of Docker build or you can even mount it while running the container.

Setup TZ during build:

FROM python:3.8
LABEL version="0.1"
WORKDIR /app
COPY . /app
RUN  ln -sf /usr/share/zoneinfo/America/New_York /etc/timezone && \
     ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime && \
     pip install -r requirements.txt
CMD ["sh", "-c", "python3 run_scheduler.py "]

Mount timezone while running container:

docker run --rm -it -v /usr/share/zoneinfo/America/New_York:/etc/timezone:ro -v /usr/share/zoneinfo/America/New_York:/etc/localtime:ro <image_name>

推荐阅读