Fact
Spring boot JAR can load profile from -Dspring.profiles.active
flag, but not from SPRING_PROFILES_ACTIVE
environment variable (WAR works fine).
How to reproduce it
Create a new JAR project from
Then mvn package
and get the JAR in target
.
$ java -jar demo-0.0.1-SNAPSHOT.jar
...
2020-04-23 00:44:20.885 INFO 5420 --- [ main] demo.demo.DemoApplication : No active profile set, falling back to default profiles: default
java -jar -Dspring.profiles.active=special demo-0.0.1-SNAPSHOT.jar
...
2020-04-23 00:44:56.093 INFO 5449 --- [ main] demo.demo.DemoApplication : The following profiles are active: special
However:
$ SPRING_PROFILES_ACTIVE=superspecial
$ echo $SPRING_PROFILES_ACTIVE
superspecial
$ java -jar demo-0.0.1-SNAPSHOT.jar
...
2020-04-23 00:45:49.545 INFO 5567 --- [ main] demo.demo.DemoApplication : No active profile set, falling back to default profiles: default
Why it is important
In the docker world, I'd like a shared docker image used by dev/staging/production environments. Therefore, I prefer to pass in SPRING_PROFILES_ACTIVE
through docker run -env
.
I can't do
ENTRYPOINT java -jar -Dspring.profiles.active=superspecial demo.jar
because then that profile setting will be baked into the image.
I also can't do
ENTRYPOINT java -jar -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE demo.jar
because ENTRYPOINT can't see the environment passed in by -env
.
Comment From: bclozel
Hello @ozooxo
I believe you can set the env variable directly like this:
SPRING_PROFILES_ACTIVE=superspecial java -jar demo-0.0.1-SNAPSHOT.jar
Or you can also export SPRING_PROFILES_ACTIVE=superspecial
to make it an actual env variable and then run the executable jar.
Now docker files provide a dedicated ENV keyword for this case, and you can set the value when calling the run command on the docker CLI.
For further questions about this topic, please use StackOverflow. Thanks!
Comment From: ozooxo
Thanks!