【问题标题】:Change time zone for docker and Dockerfile更改 docker 和 Dockerfile 的时区
【发布时间】:2020-11-25 04:30:01
【问题描述】:

我有一个 AWS EC2 实例,我计划在太平洋标准时间晚上 11:50 打开并在太平洋标准时间晚上 11:59 关闭。我已将实例的时区设置为 MST,以便我可以在 MST 晚上 11:55 运行一个执行 .sh 文件的 cron 作业。 cron 任务非常简单:55 23 * * * sudo bash docker run --mount type=bind,source="/home/ec2-user/environment/Project",target="/Project" myubuntu。 docker 将挂载到本地文件夹“Project”,该文件夹包含一个 .cpp 文件,该文件从 Steam 的用户信息页面抓取数据。 .cpp 文件中的代码非常依赖于当前时间/日期,因此我做了这么多工作来让所有东西都在 MST 中运行,以便一切都是标准的。然而,即使一切都在 MST 上运行,当 docker 容器运行时它不在 MST 中,尽管 dockerfile 声明使用ENV TZ="America/Salt Lake City" 运行,我已经将它从盐湖城更改为凤凰城只是为了尝试它,但它仍然不在 MST 中运行 docker。例如,当我在 MST 11 月 24 日晚上 9:22 运行 docker 时,docker 中的日期是 11 月 25 日凌晨 04:22 UTC。这种细微的日期和时间更改极大地影响了我尝试运行的代码。

为了解释代码的作用,Steam 有一个 .json URL,它在“[unix epoch time, # users logged in]”中保存了大约 48-62 小时的数据。目标是自动化,所以我想如果我让代码删除任何与代码运行日期不匹配的数据,它就不会包含在数据收集中。因此,我通过每天结束时运行代码来一次收集 24 小时的数据。我和我的 EC2 实例运行的 MST 时间与我的 docker 运行的 UTC 时间之间的日期/时间差异导致数据收集问题。

我的教授给了我 dockerfile,据说它被设置为在 MST 上运行,但据我所知,它不是。我尝试使用包含的-v /etc/timezone:/etc/timezone 在我的 .sh 文件中运行我的命令,但这似乎也不能解决时区问题。我得到的dockerfile如下:

# This image will be based on the ubuntu image.  Build it using the
# command in the comment below.  You may omit the ubuntu pull if you already
# did it.  Before running the build, change into the directory containing
# this Dockerfile.
FROM ubuntu

# Set a default shell.
SHELL ["/bin/bash", "-c"]

# The timezone library stops the docker build and waits for user input to
# select a timezone. This breaks the build. To get around this, 
# set up timezone information prior to installing that library. This
# Docker code does that. Composited from two sources:
# https://rtfm.co.ua/en/docker-configure-tzdata-and-timezone-during-build/
# https://serverfault.com/questions/949991/how-to-install-tzdata-on-a-ubuntu-docker-image
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ="America/Phoenix"

# Install a handful of useful libraries.  Note that this is generally against
# best practices -- containers should be lean, and not include 'things you
# might use'.  In our case, we're using the containers for development, so
# this may be reasonable here.
RUN apt-get -y update && apt-get -y install \
  apt-utils \
  emacs-nox \
  g++

# Copy in the files from the current folder (recursively) For our purposes,
# put them in /cs3505
COPY . /cs3505

RUN apt-get -y install wget

我或我的教授在设置 docker 时是否做错了什么导致了这个时区问题?我该如何修复我的 docker,以便每次在 MST 晚上 11:55 运行时,它都会以 MST 作为时区打开?

编辑:我不知道这是否会有所不同,但运行 cat /etc/timezone 会返回“United States/Mountain”,而运行 emacs /etc/timezone 会显示相同的结果。

【问题讨论】:

  • 时区通常是有问题的,因为您需要确保代码中涉及的所有内容都设置为该时区。更好的方法是将所有内容设置为 GMT 并将所需的限制转换为 GMT 并运行它。

标签: docker datetime ubuntu docker-compose dockerfile


【解决方案1】:

这是我基于Debian定制的dockerfile,可以参考:

FROM debian:stable-slim

ARG ARG_TIMEZONE=Asia/Shanghai
ENV ENV_TIMEZONE                ${ARG_TIMEZONE}

# install base dependence
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections \
    && apt-get update && apt-get install -y -q \
        dialog apt-utils \
        locales systemd cron \
        vim wget curl exuberant-ctags tree \
        tzdata ntp ntpstat ntpdate \
    && apt-get clean && apt-get autoremove -y && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

# sync timezone
RUN echo '$ENV_TIMEZONE' > /etc/timezone \
    && ln -fsn /usr/share/zoneinfo/$ENV_TIMEZONE /etc/localtime \
    && dpkg-reconfigure --frontend noninteractive tzdata

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多