|
| 1 | +# Spring Boot Overriding Environment Variables Guide |
| 2 | +This is an example of overriding the application.yml file with the OS environment variables. `Single` or `Array` environment variables can be overridden. |
| 3 | + |
| 4 | +## Overview |
| 5 | +1. This guide will show you how to override environment variables in a Spring Boot application. |
| 6 | +2. Single and multiple environment variables will be overridden. |
| 7 | +3. When you run a Spring Boot application, the application properties are converted to environment variables. |
| 8 | +4. You can override environment variables using the `-D` flag when java -jar is used. |
| 9 | +5. You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container. |
| 10 | +6. You can override environment variables using ConfigMaps and Secrets when running a Spring Boot application in a Kubernetes cluster. |
| 11 | +7. You can override environment variables using the `application.yml` file from git repository with spring cloud config server. |
| 12 | +--- |
| 13 | + |
| 14 | +## Our Use Case |
| 15 | +4 and 5 are the most common use cases for overriding environment variables in a Spring Boot application. Maybe next time we can talk about the Kubernetes and Spring Cloud Config Server use cases. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Environments Naming Convention |
| 20 | +When you run a Spring Boot application, the application properties are converted to environment variable like UPPER_CASE with underscores. |
| 21 | + |
| 22 | +* application.yml file |
| 23 | +```yml |
| 24 | +app-properties: |
| 25 | + name: "Spring Boot Overriding Environment Variables" |
| 26 | + version: "0.0.1" |
| 27 | + description: "Spring Boot Overriding Environment Variables with application.yml" |
| 28 | + author: "Cevheri" |
| 29 | + email: "cevheribozoglan@gmail.com" |
| 30 | +``` |
| 31 | +* OS Environment Variable |
| 32 | +```shell |
| 33 | +APP_PROPERTIES_NAME=overridden-name |
| 34 | +APP_PROPERTIES_VERSION=overridden-version |
| 35 | +APP_PROPERTIES_DESCRIPTION=overridden-description |
| 36 | +APP_PROPERTIES_AUTHOR=overridden-author |
| 37 | +APP_PROPERTIES_EMAIL=overridden-email |
| 38 | +``` |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Use Cases |
| 43 | +Environment variables are key-value pairs that are set in the operating system. |
| 44 | +They are used to configure applications and services. |
| 45 | + |
| 46 | +### java -jar |
| 47 | +When you run a Spring Boot application using the `java -jar` command, you can override environment variables using the `-D` flag. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### docker run |
| 52 | +When you run a Spring Boot application in a Docker container, you can override environment variables using the `-e` flag. |
| 53 | + |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Build |
| 58 | +### Maven Build |
| 59 | +```shell |
| 60 | +mvn clean install |
| 61 | +``` |
| 62 | + |
| 63 | +### Build Docker Image |
| 64 | +```shell |
| 65 | +docker build -t soe:latest . |
| 66 | +``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Run |
| 71 | +Run the Spring Boot application using the `java -jar` command. |
| 72 | + |
| 73 | +### Java |
| 74 | +#### Simple |
| 75 | +```shell |
| 76 | +java -jar target/soe.jar |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +#### Override Environment Variables |
| 82 | +```shell |
| 83 | +java -jar '-Dapp-properties.name=overridden-name-cevheri' '-Dapp-roles[0]=overridden-role-0-cevheri' '-Dapp-paths[0].roles[4]=overridden-path-0-role-0-cevheri' target/soe.jar |
| 84 | +``` |
| 85 | +### Docker |
| 86 | +#### Simple |
| 87 | +```shell |
| 88 | +docker run -p 8080:8080 soe:latest |
| 89 | +``` |
| 90 | + |
| 91 | +#### Override Environment Variables |
| 92 | +```shell |
| 93 | +docker run -p 8080:8080 -e APP_PROPERTIES_NAME=overridden-name-cevheri-docker -e APP_ROLES_0=overridden-role-0-cevheri-docker -e APP_PATHS_0_ROLES_4=overridden-path-0-role-0-cevheri-docker soe:latest |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +### Reference Documentation |
| 98 | + |
| 99 | +For further reference, please consider the following sections: |
| 100 | +* https://spring.io/guides/gs/spring-boot |
| 101 | +* https://docs.spring.io/spring-boot/reference/features/external-config.html |
| 102 | +* https://maven.apache.org/guides/index.html |
| 103 | +* https://docs.spring.io/spring-boot/3.3.2/maven-plugin |
| 104 | +* https://docs.spring.io/spring-boot/3.3.2/maven-plugin/build-image.html |
| 105 | +* https://docs.docker.com/compose/environment-variables/set-environment-variables/ |
0 commit comments