[英]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. 我有一个Spring Boot应用程序,打算将其部署为Docker容器。
I'm using a DOCKERFILE to build the image with entrypoint: ENTRYPOINT ["java", "-jar", "myFolder/app.jar"]
我正在使用DOCKERFILE构建带有入口点的图像:
ENTRYPOINT ["java", "-jar", "myFolder/app.jar"]
The image is buildt in a JENKINSFILE like this: docker build . -t repo/app:latest
映像将在JENKINSFILE中构建,如下所示:docker
docker build . -t repo/app:latest
docker build . -t repo/app:latest
I'm using a script to run the docker image. 我正在使用脚本来运行docker映像。 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
. 假设我有一个自定义属性:
custom.property.isTest=false
。 It controls which class a bean should return an instance of eg 它控制bean应该返回哪个类的实例,例如
@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. 我希望能够在运行docker映像时设置此值。 eg using the parameter:
-e
to do something like this(doesn't work) 'custom.property.isTest=true'
. 例如,使用参数:
-e
做类似的事情(不起作用) 'custom.property.isTest=true'
。 Is that possible? 那可能吗?
Thanks 谢谢
Yes you can pass this variable like -e UPPERCASE_OF_YOUR_PROPERTY
: 是的,您可以像
-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.
我建议阅读Spring Boot文档的Externalized Configuration部分 。 I copy only relevant options:
我只复制相关选项:
- Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
您的主目录上的Devtools全局设置属性(在devtools处于活动状态时,为〜/ .spring-boot-devtools.properties)。
- Command line arguments.
命令行参数。
- Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
来自SPRING_APPLICATION_JSON的属性(嵌入在环境变量或系统属性中的嵌入式JSON)。
- Java System properties (System.getProperties()).
Java系统属性(System.getProperties())。
- OS environment variables.
操作系统环境变量。
- Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
打包的jar之外的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)。
- Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
打包在jar中的特定于配置文件的应用程序属性(application- {profile} .properties和YAML变体)。
- Application properties outside of your packaged jar (application.properties and YAML variants).
打包的jar之外的应用程序属性(application.properties和YAML变体)。
- Application properties packaged inside your jar (application.properties and YAML variants).
打包在jar中的应用程序属性(application.properties和YAML变体)。
In the docker file, where you run the command ...java -jar myapp.jar...
you should be able to pass the -Dcustom.property.isTest=false
. 在docker文件中,运行命令
...java -jar myapp.jar...
您应该能够传递-Dcustom.property.isTest=false
。 If you can provide the snippet of your docker file, that will be helpful. 如果您可以提供docker文件的代码段,将很有帮助。 The parameters might be in quotes (eg.
CMD java -jar myapp.jar "-DisTest=false"
参数可能用引号引起来(例如
CMD java -jar myapp.jar "-DisTest=false"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.