Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring.config.additional-location as environmental variable

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.

like image 984
GionJh Avatar asked Feb 19 '19 18:02

GionJh


People also ask

How do I set environment specific properties in Spring boot?

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.

What are the possible ways to provide an external configuration in Spring boot?

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.


1 Answers

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

like image 105
Feli Avatar answered Oct 06 '22 22:10

Feli