编辑:
我从头开始我的 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 环境变量。
环境:
重现错误的步骤:
我已经采取的步骤和尝试(在融化之前)
另外,如果有开发人员或有人可以回答我的问题,但这是临时性的,这里有:
这是我后端的 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
如果有人帮忙,我将不胜感激。
我也遇到同样的问题,请问你是怎么解决的??