如何修复 Docker Build 生成的容器中的时区

问题描述 投票:0回答:3

抱歉,我是

docker
docker-compose
的新手。

“已保存”容器未显示正确的时区。

背景:

  1. 由于公司限制,我无法将 Dockerized SpringBoot 应用程序放置到主机 B。
  2. 我的老板告诉我在主机 A 中 Dockerized SpringBoot 应用程序。
  3. docker save $imageName > application.tar
    保存在主机 A 中
  4. 将保存的图片
    docker load < application.tar
    加载到主机B
  5. 在主机 B 中运行 Docker 镜像...
  6. 主机A和主机B处于同一时区(香港时间)

结果:

  1. 容器内时区(检查日志结果)发现主机A中的日志时间是正确的(香港时区,UTC+8) (无论是

    docker run
    还是
    docker compose
    触发)

  2. 容器内时区(检查日志结果)发现主机B中日志时间错误(UTC+0) (无论是

    docker run
    还是
    docker compose
    触发)

docker版本 客户: 版本:1.13.1 API版本:1.26 软件包版本:docker-1.13.1-88.git07f3374.el7.x86_64 Go版本:go1.10.2 Git 提交:07f3374/1.13.1 建成时间:2018 年 12 月 6 日星期四 07:01:49 操作系统/架构:linux/amd64

docker-compose 版本 docker-compose 版本 1.23.2,内部版本 1110ad01 docker-py版本:3.6.0 CPython版本:3.6.7 OpenSSL 版本:OpenSSL 1.1.0f 2017 年 5 月 25 日

托管 Ubuntu 版本 18.04.3

FROM java:8-jdk-alpine
WORKDIR /root/flexi/
COPY ./target/foo.jar /root/flexi/

### an alpine based image you have to install the tzdata first #### 
RUN apk add --no-cache tzdata


### ENV TZ=Asia/Hong_Kong
### RUN echo "Europe/Stockholm" > /etc/timezone
### RUN dpkg-reconfigure -f noninteractive tzdata
### Not work for ubuntu to dpkg-reconfigure


VOLUME /log

ADD db.properties /root/flexi
EXPOSE 9988
RUN sh -c 'touch foo.jar'
ENTRYPOINT ["java", "-jar", "foo.jar"]
version: '2.2'
services:
  foos:
    build:
      context: ./
      dockerfile: Dockerfile
    image: foos
    ports:
      - "9555:9988"
    environment:
      - TZ=Asia/Hong_Kong
    networks:
      - network1
    volumes:
      - /log:/log
networks:
  network1:
docker build -t foos .
docker inspect -f '{{ .Created }}'  foos
--> Shows UTC time (in both Host A and Host B)
docker timezone ubuntu-18.04
3个回答
1
投票

使用环境变量设置时区

  • 可以使用环境变量设置容器的时区

docker run -e TZ=美国/纽约 ubuntu 日期

  • 容器中需要安装时区数据包tzdata
  • 配置 NTP 服务器以确保容器中的时区同步

0
投票

解决方案:

# install tzdata ref. https://serverfault.com/a/992421/41015
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install tzdata

RUN cp -r -f /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

如果不运行此命令,构建映像不包含/etc/timezone,因此它使用UTC时间。


详情

### COPY /etc/timezone      /ect/timezone
### fail. It cannot copy the file to the container internally

################################################
## No luck to execute dpkg-reconfigure for UBUNTU 18.0.4.3
################################################
## ENV DEBIAN_FRONTEND=noninteractive
### RUN dpkg-reconfigure --frontend noninteractive tzdata
OR
### RUN sudo dpkg-reconfigure --frontend noninteractive tzdata
################################################

################################################
## no use
################################################
ENV TZ=Asia/Hong_Kong
### RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
################################################

################################################
## no use
################################################
## RUN echo "Asia/Hong_Kong" > /config/etc/timezone
################################################

0
投票

对于那些运行带有 Spark 上下文的容器的人,您需要 将 env 变量添加到 Spark 上下文

在 databricks 中,您可以通过 spark_env_vars 来完成此操作,更多文档也在这里

例如:

TZ="UTC"

© www.soinside.com 2019 - 2024. All rights reserved.