Hi everyone,
I am experiencing a problem/issue with my Spring Boot base application while running it in a Docker container using Docker Compose.
While the application is launched using the following command: docker-compose pull && docker-compose up -d, the container is unable to find the active profiles, the Spring profiles are not taken into account but while not using the -d, it works just fine though I’m stuck with my terminal.
To be more precise about the problem, my Spring Boot application behaves differently in detached mode compared to the “normal way”, specifically regarding the activation of Spring profiles. In detached mode, the application does not seem to recognize the SPRING_PROFILES_ACTIVE environment variable.
Environment:
- Spring Boot version: 3.0.6
- Docker version: Ubuntu 22.04 64bit with Docker
- Docker-compose version: v2.21.0
Steps to reproduce the bug:
- Run docker-compose up - The application works fine, and the profiles are active.
- Run docker-compose up -d - The application starts, but the profiles are not active (fall back to the default profile).
My already made steps and tries (before melting away)
- Checked environment variables inside the container.
- Compared logs in both modes.
- Tried to pass the environment variables (SPRING_PROFILES_ACTIVE) in my docker compose file with and without the commas
Also, if there are dev ops or someone who can answer my questions, but it’s facultative, here there are:
- Why would the Spring profiles not be recognized when running the Docker Compose in detached mode?
- Is there a difference in how environment variables or application properties are processed between these two modes? Have any suggestions on how to ensure that the profiles are consistently recognized regardless of the mode in which Docker Compose runs?
Here is my Dockerfile from my backend:
# 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"]
My docker compose file, which is located in the root of the folder:
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:
Here is my application.properties file:
application.properties:
spring.profiles.group.dockerLocal=dockerLocal
spring.profiles.group.secretProd=secretProd
I would appreciate it if anyone helps.