|
1 | 1 | # springboot-properties-docker |
2 | | -How to use springboot properties in docker or docker-compose |
| 2 | +This example shows how to use springboot properties in docker or docker-compose |
| 3 | + |
| 4 | +In spring boot, variable processing priority is given according to the position of the property variable. |
| 5 | + |
| 6 | + |
| 7 | +##### The rest url that validates the propertis variable looks like this: |
| 8 | + |
| 9 | +```shell |
| 10 | +http://localhost:8080/dynamicvalue |
| 11 | +--- |
| 12 | +curl -GET http://localhost:8080/dynamicvalue |
| 13 | +``` |
| 14 | + |
| 15 | +## How to use |
| 16 | + |
| 17 | +1.maven build package jar |
| 18 | +```shell |
| 19 | +mvn clean package -DskipTest |
| 20 | +``` |
| 21 | + |
| 22 | +2.CASE[1]: Run application and check variable 'dynamic.value' in application.properties[1] |
| 23 | + |
| 24 | +```shell |
| 25 | +java -jar ./target/springboot-properties-docker-0.0.1.jar |
| 26 | + |
| 27 | +``` |
| 28 | +* result : helloworld |
| 29 | + |
| 30 | +3.CASE[2]:Add the dynamic.value variable to the OS environment variable and run this application.[2] |
| 31 | +```shell |
| 32 | +> env dynamic.value=this_is_os_variable bash |
| 33 | +> java -jar ./target/springboot-properties-docker-0.0.1.jar |
| 34 | +``` |
| 35 | +* result : this_is_os_variable |
| 36 | + |
| 37 | +4.CASE[3]:Add a variable to the command line when jar file excution |
| 38 | +```shell |
| 39 | +> java -jar ./target/springboot-properties-docker-0.0.1.jar --dynamic.value=commandline_variable |
| 40 | +``` |
| 41 | + |
| 42 | +* result : commandline_variable |
| 43 | + |
| 44 | +## In Docker image Configuration |
| 45 | +In this Dockerfile, |
| 46 | +This image extends centos7 and jdk1.8 and container has its own environment. |
| 47 | +It would be enough to declare what you want to override as environment properties and Spring Boot will fetch them, since environment variables take precedence over the yml files. |
| 48 | + |
| 49 | +##### 1.Docker build app |
| 50 | + |
| 51 | +```shell |
| 52 | +> docker build -t test/test . |
| 53 | +``` |
| 54 | + |
| 55 | +##### 2.Docker run app in command |
| 56 | + |
| 57 | +```shell |
| 58 | +> docker run -rm -p8080:8080 -e dynamic.value=this_is_docker_os_variable test/test |
| 59 | +``` |
| 60 | +* result : this_is_docker_os_variable |
| 61 | + |
| 62 | +##### 3.Docker-compose sample |
| 63 | +```shell |
| 64 | +> docker-compose -f ./docker-compose.yml up |
| 65 | + |
| 66 | + |
| 67 | +--- |
| 68 | +# after test |
| 69 | +> docker-compose stop |
| 70 | +> docker-cmpose rm |
| 71 | +``` |
| 72 | +* result : this_is_docker_os_variable |
| 73 | + |
| 74 | +Here you have an example of how I launch a simple app environment with docker compose. As you see, I declare the 'dynamic.value' property here as an environment variable, so it overrides whatever you've got inside jar application.properties file. |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +### The priority rank according to the properties position is as follows. |
| 79 | +```text |
| 80 | +1. 'Spring-boot-dev-tools.properties' file in the user's home directory |
| 81 | +2. @TestPropertySource |
| 82 | +3. The properties attribute of the @SpringBootTest annotation |
| 83 | +4. Command line arguments <-------[3] |
| 84 | +5. Properties in SPRING_APPLICATION_JSON (environment variable or system property) |
| 85 | +6. ServletConfig Parameters |
| 86 | +7. ServletContext parameter |
| 87 | +8. java:comp/env JNDI attribute |
| 88 | +9. System.getProperties () Java System Properties |
| 89 | +10. OS environment variables <-------[2] |
| 90 | +11. RandomValuePropertySource |
| 91 | +12. Application properties for a specific profile outside the JAR |
| 92 | +13. Application properties for a specific profile in the JAR |
| 93 | +14. Application properties outside the JAR |
| 94 | +15. Application properties in the JAR <------[1] |
| 95 | +16. @PropertySource |
| 96 | +17. Default property (SpringApplication.setDefaultProperties) |
| 97 | +``` |
0 commit comments