主机时区不反映在 spring-boot docker 容器上

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

我正在尝试将我的 docker 容器的本地时间设置为与本地本地时间相同,以便在保存数据时在 spring-boot 应用程序上使用。

spring-boot 应用程序:

...
cpoWorkflowExecution.setStartDate(LocalDateTime.now());
...

当地时间:

[ainacio@brsp1nf01 cpo-dockers]$ ls -l /etc/localtime
lrwxrwxrwx. 1 root root 39 Oct  6 07:42 /etc/localtime -> ../usr/share/zoneinfo/America/Sao_Paulo

当地时区:

[ainacio@brsp1nf01 cpo-dockers]$ cat /etc/timezone 
America/Sao_Paulo

容器本地时间不起作用:

root@ffbd68eeaccd:/# ls -l /etc/localtime
lrwxrwxrwx. 1 root root 27 Nov 17 13:37 /etc/localtime -> /usr/share/zoneinfo/Etc/UTC

容器时区有效:

root@ffbd68eeaccd:/# cat /etc/timezone 
America/Sao_Paulo

Docker 撰写:

version: '3'
services:
    cpo-executor:
        container_name: cpo-executor
        image: .../cpo-executor:1.103.3.1
        ports:
          - 8879:8879
        environment:
          - TZ=America/Sao_Paulo
        volumes:
          - /etc/timezone:/etc/timezone:ro
          - /etc/localtime:/etc/localtime:ro
        networks:
          - cpo-network
        depends_on:
          - cpo-config-server

我也尝试过设置 TZ 环境变量,但效果不佳。

我正在使用 spring-boot maven 插件生成 docker 映像:

mvn spring-boot:build-image -DskipTests -Dspring-boot.build-image.imageName=.../cpo-executor:1.103.3.1

我错过了什么?

spring-boot docker docker-compose timezone localtime
1个回答
0
投票

如果您使用 Spring Boot Maven Plugin 并使用

mvn spring-boot:build-image
构建镜像,您可以将此设置添加到您的 pom 中,以便覆盖
TZ
环境变量。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <image>
                    <env>
                        <BPE_TZ>Asia/Tehran</BPE_TZ>
                    </env>
                </image>
            </configuration>
        </plugin>
    </plugins>
</build>

点击这些链接:

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