I have a Docker image for a spring boot 2 app, in the image I use spring.config.additional-location to pass a some properties stored in a file on the container itself.
Now I want to use this container in another as a layer in another container or use it in a docker-compose file and I may need to override the spring.config.additional-location.
I tried to override it in an ENV variable:
SPRING_CONFIG_ADDITIONAL-LOCATION
with no success.
What is the proper way to achieve this.
Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.
Value for spring.config.additional-location must be a directory path and not a file path. I'm not sure if this is a bug or a feature, since spring.config.location allows both.
For example inside docker-compose.yml you can set spring.config.additional-location as:
version: '3'
services:
myapp:
build: .
environment:
- "SPRING_CONFIG_ADDITIONAL-LOCATION=file:/opt/myapp/config/"
- "SPRING_PROFILES_ACTIVE=production"
...
Then Spring Boot will look up for /opt/myapp/config/application-production.properties file to load properties from.
More about this in Spring Boot Externalized Configuration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With