3

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

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 e.g

@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. e.g using the parameter: -e to do something like this(doesn't work) 'custom.property.isTest=true'. Is that possible?

Thanks

mTv
  • 1,074
  • 17
  • 28

3 Answers3

6

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
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
  • Thanks. This is what I was looking for. Is it better to do it like this or through DOCKERFILE? are there any best practices? – mTv Aug 02 '18 at 15:07
  • I think you can give it when run image. If you want to run multiple image with diffrent value then u need to give it run time – GolamMazid Sajib Aug 02 '18 at 15:09
  • I was looking that the DOCKERFILE solution people suggested. I would have to add parameters to my JENKINSFILE, then pass it as a `--build-arg` to the `docker build` command and then use that in my entrypoint. This seems so much easier. – mTv Aug 02 '18 at 15:11
1

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).
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

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"

alltej
  • 6,787
  • 10
  • 46
  • 87