简体   繁体   中英

How can i set Spring Boot properties from Docker 'run' command

I have a Spring Boot application which i intent to deploy as a docker container.

I'm using a DOCKERFILE to build the image with entrypoint: ENTRYPOINT ["java", "-jar", "myFolder/app.jar"]

The image is buildt in a JENKINSFILE like this: docker build . -t repo/app:latest docker build . -t repo/app:latest

I'm using a script to run the docker image. I want to set a custom property's value based on an argument to that script.

So say I have a custom property: custom.property.isTest=false . It controls which class a bean should return an instance of eg

@Value("${custom.property.isTest:false}")
boolean isTest;

@Bean
public MyService myServiceImpl(){
    if(isTest) {
        return new myServiceTestImpl();
    } else {
        return new myServiceImpl();
    }
}

I want to be able to set this value when I run the docker image. eg using the parameter: -e to do something like this(doesn't work) 'custom.property.isTest=true' . Is that possible?

Thanks

Yes you can pass this variable like -e UPPERCASE_OF_YOUR_PROPERTY :

Example:

docker run -d --name servie-name -e CUSTOM_PROPERTY_ISTEST=true  -p port:port image:tag

You have bunch of options. I recommend to read Externalized Configuration section of Spring Boot docs. I copy only relevant options:

  • Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  • Command line arguments.
  • Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  • Java System properties (System.getProperties()).
  • OS environment variables.
  • Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  • Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  • Application properties outside of your packaged jar (application.properties and YAML variants).
  • Application properties packaged inside your jar (application.properties and YAML variants).

In the docker file, where you run the command ...java -jar myapp.jar... you should be able to pass the -Dcustom.property.isTest=false . If you can provide the snippet of your docker file, that will be helpful. The parameters might be in quotes (eg. CMD java -jar myapp.jar "-DisTest=false"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM