[英]How can I set parameters from docker command line to configure spring boot application?
I have my spring boot application with a yml to configure it inside a docker containter. 我有一个带有yml的spring boot应用程序,可以在docker containter中配置它。 Something like that: 像这样的东西:
spring: application:
name: micro-example
config:
uri: ${vcap.services.config-service.credentials.uri:http://xxx.xxx.xx.73:8888}
As you can see, there is an ip hardcoded in my config, that is a bad idea, because the compililation is just for a server. 正如你所看到的,我的配置中有一个ip硬编码,这是一个坏主意,因为编译仅适用于服务器。 Does a way exist to externalize the ip, or set it from a docker command line or a better idea? 是否存在外部化ip的方法,或者从docker命令行或更好的想法设置它?
There many different ways to do it: 有很多不同的方法可以做到:
1) Set environment variable (use export VCAP_SERVICES_CONFIG-SERVICE_CREDENTIALS_URI='http://example.com'
in shell, or ENV
inside Dockerfile
) 1)设置环境变量(在shell中使用export VCAP_SERVICES_CONFIG-SERVICE_CREDENTIALS_URI='http://example.com'
,或在Dockerfile
ENV
)
2) Pass it as JVM argument ( java -Dvcap.services.config-service.credentials.uri=http://example.com -jar app.jar
) 2)将其作为JVM参数传递( java -Dvcap.services.config-service.credentials.uri=http://example.com -jar app.jar
)
3) Pass it as command line argument ( java -jar app.jar --vcap.services.config-service.credentials.uri=http://example.com
) 3)将其作为命令行参数传递( java -jar app.jar --vcap.services.config-service.credentials.uri=http://example.com
)
4) Spring Boot also reads values from config/application.properties
or application.properties
that are located in the same directory as your executable JAR file, so it's possible to provide this file (you could use VOLUME
for that) and override settings from JAR 4)Spring Boot还从config/application.properties
或application.properties
读取值,这些值与可执行JAR文件位于同一目录中,因此可以提供此文件(可以使用VOLUME
)并覆盖JAR中的设置
See also: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 另见: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
You basically have a few options from my perspective: 从我的角度来看,你基本上有一些选择:
docker run -e SPRING_CONFIG_URI="http://xxx.xxx.xx.73:8888 user/image:tag"
将params作为来自docker run -e SPRING_CONFIG_URI="http://xxx.xxx.xx.73:8888 user/image:tag"
环境变量传递,例如通过使用以下命令启动应用程序: docker run -e SPRING_CONFIG_URI="http://xxx.xxx.xx.73:8888 user/image:tag"
docker run --name configserver my/configserver:latest
and start your service then using linking docker run --link configserver:configserver my/service:latest
. 使用相同的主机名并使用容器链接,为您的第一个容器命名为docker run --name configserver my/configserver:latest
并启动您的服务,然后使用链接docker run --link configserver:configserver my/service:latest
。 Caveat is that you'll be bound to the same docker host. 警告是你将被绑定到同一个docker主机。 http://configserver:8888
in your application and start the container with docker run --add-host configserver:xxx.xxx.xx.73 user/image:tag
始终使用相同的主机名和端口,并在容器启动时注入正确的主机名映射,例如在应用程序中配置http://configserver:8888
并使用docker run --add-host configserver:xxx.xxx.xx.73 user/image:tag
启动容器docker run --add-host configserver:xxx.xxx.xx.73 user/image:tag
docker run --dns=your-dns-server user/image:tag
and register your dependent services in a dns server that can have dynamic configurations, like consul or SkyDNS + etcd . 通过docker run --dns=your-dns-server user/image:tag
注入一个可以作为运行容器的DNS服务器的自定义服务,并在dns服务器中注册您的依赖服务,该服务器可以具有动态配置,如consul或SkyDNS + etcd 。 Whereas the last approach has the benefit that you dynamically link containers together cross nodes hosting docker containers. 而最后一种方法的好处是,您可以将容器动态链接到托管泊坞容器的跨节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.