已解决 - 在分离模式下运行 docker compose 时 Spring 启动配置文件不活动

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

编辑:

我从头开始我的 VPS 并修改了我的 Dockerfile:

# Amazon Corretto with Java 20.0.2 as the base image
FROM amazoncorretto:20.0.2

# Metadata as a label
LABEL maintainer="[email protected]" version="0.0.1-SNAPSHOT" description="Santa Secret project"

# Declare JAR_FILE as a variable. Default value is "work-0.0.1-SNAPSHOT.jar", but it can be overridden during Docker build.
ARG JAR_FILE=target/work-0.0.1-SNAPSHOT.jar

# Copy the JAR file specified by JAR_FILE into the container as app.jar
COPY ${JAR_FILE} app.jar

# Specify the base command to run the jar
#"-Dspring.profiles.active=prod",
ENTRYPOINT ["java","-Dlogging.debug=true","-jar","/app.jar"]

# Set the SPRING_PROFILES_ACTIVE environment variable
ARG SPRING_PROFILES_ACTIVE
ENV SPRING_PROFILES_ACTIVE=$SPRING_PROFILES_ACTIVE

结束编辑

我在使用 Docker Compose 在 Docker 容器中运行 Spring Boot 基础应用程序时遇到了问题。

当使用以下命令启动应用程序时:docker-compose pull && docker-compose up -d,容器无法找到活动配置文件,不会考虑 Spring 配置文件,但在不使用 -d 时,它尽管我被终端困住了,但工作得很好。

更准确地说,我的 Spring Boot 应用程序在分离模式下的行为与“正常方式”不同,特别是在 Spring 配置文件的激活方面。在分离模式下,应用程序似乎无法识别 SPRING_PROFILES_ACTIVE 环境变量。

环境:

  • Spring Boot版本:3.0.6
  • Docker 版本:Ubuntu 22.04 64 位,带 Docker
  • Docker-compose 版本:v2.21.0

重现错误的步骤:

  • 运行 docker-compose up - 应用程序运行良好,并且配置文件 很活跃。
  • 运行 docker-compose up -d - 应用程序启动,但配置文件 未激活(回退到默认配置文件)。

我已经采取的步骤和尝试(在融化之前)

  • 检查容器内的环境变量。
  • 比较两种模式下的日志。
  • 尝试在我的 docker compose 文件中传递带或不带逗号的环境变量(SPRING_PROFILES_ACTIVE)

另外,如果有开发人员或有人可以回答我的问题,但这是临时性的,这里有:

  • 为什么运行 Docker 时无法识别 Spring 配置文件 以分离模式撰写?
  • 环境变量或应用程序的方式有区别吗 属性在这两种模式之间进行处理? 对于如何确保配置文件一致有任何建议 无论 Docker Compose 的运行模式如何都能识别?

这是我后端的 Dockerfile:

# Amazon Corretto with Java 20.0.2 as the base image
FROM amazoncorretto:20.0.2

# Metadata as a label
LABEL maintainer="anonymized" version="0.0.1-SNAPSHOT" description="Santa Secret project"

# Declare JAR_FILE as a variable
ARG JAR_FILE=target/work-0.0.1-SNAPSHOT.jar

# Copy the JAR file into the container
COPY ${JAR_FILE} app.jar

# Specify the base command to run the jar
ENTRYPOINT ["java","-jar","/app.jar","--spring.profiles.active=dockerLocal,secretProd"]

我的 docker compose 文件,位于文件夹的根目录中:

version: '3.8'

services:
  backend:
    image: myHubName/secretsanta-backend:latest
    ports:
      - "8080:8080"
    environment:
      SPRING_PROFILES_ACTIVE: "dockerLocal,secretProd"
      SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/${POSTGRES_DB}
      SPRING_DATASOURCE_USERNAME: ${POSTGRES_USER}
      SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD}
    depends_on:
      - database

  frontend:
    image: myHubName/secretsanta-frontend:latest
    ports:
      - "80:80"

  database:
    image: postgres:latest
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

这是我的 application.properties 文件:

application.properties: 

spring.profiles.group.dockerLocal=dockerLocal
spring.profiles.group.secretProd=secretProd

如果有人帮忙,我将不胜感激。

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

我也遇到同样的问题,请问你是怎么解决的??

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