Configuring Spring Boot Application Properties in Containerized Environment

Until a few years ago, monolithic applications were common. With numerous functions knitted into the fabric of the codebase and interdependencies running through them, it was difficult — or even impossible — to work on individual functions in isolation. To add to the complexity, managing multiple environment-specific configuration was a nightmare. That is when Spring introduced the interesting feature of creation of multiple environment profiles with /application.yml. Creating a bunch of application-[profile].properties files like application-local.yml, application-test.yml, application-dev.yml was very common in those days to create different environment-specific configurations.

Fast Forward to Today

As the technology progressed, soon organizations started adapting modern ways of not only developing the application, but also deploying them. With introduction of microservices architecture and containerization technologies like Kubernetes and Docker, there came the inherent need of managing and deploying it in a cleaner way. 

Externalizing configuration properties is one of the 12 factors and guidelines for building cloud-native applications. 

With the introduction of Kubernetes, soon the need for maintaining those bunches of environment-specific file ended and was replaced by configuration in Kubernetes manifest files, or ConfigMaps, along with secrets/Azure key vault for sensitive information.

With modern containerization technologies like Kubernetes and Docker, are multiple environment-specific Spring Boot still relevant? Not really!

All that is needed is single or application.yml with placeholder like ${AZURE_STORAGE_ENDPOINT} that will be injected with Kubernetes manifest files or ConfigMaps.

Below is an example of how application.yml with a placeholder will look. 

16041950 1657694656350

Developers’ Corner

With this, obviously developers who used to run applications with application-local.yml would be interested in knowing the way to run application in their IDE. Here is the way to do it in IntelliJ, as mentioned on

Add Environment Variables

  1. From the main menu, select Run | Edit Configurations or choose Edit Configurations from the run/debug configurations selector on the toolbar.
  2. In the Run/Debug Configurations dialog, select a configuration you want to add the environment variables to.
  3. Type the variable name and value: <name>=<value>. If you want to add a few variables, separate them with semicolons;.

To escape the semicolon, use the backslash: VAR=one\;two.

Alternatively, click the notepad icon, and add the variable name and value to the User environment variables list. 

16041960 intellij vm

16041962 1657695190197


Hopefully, these simple steps will help to centralize and externalize the configuration of Spring Boot applications in a containerized environment. Also, these steps will enable developers to stop having to maintain a bunch of environment-specific application.yml/ files. Only a single application.yml/ file with placeholders like ${PROPERTY_VALUE} to inject values of the properties run time is needed. The same will be injected by container management platforms like Kubernetes. This will help with “Externalizing Configuration Properties,” which is one of the 12 factors and guidelines for building cloud-native applications.

News Credit

%d bloggers like this: