简体   繁体   English

如何从docker命令行设置参数以配置spring boot应用程序?

[英]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.propertiesapplication.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: 从我的角度来看,你基本上有一些选择:

  1. Pass in params as environment variables from docker, eg by starting the app using: 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"
  2. Use the same hostname and use container linking by giving your first container a name 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主机。
  3. Use the same hostname and port all the time and inject the correct hostname mapping on startup of the container, eg configure 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
  4. Inject a custom service that can act as a DNS server to the running container by 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服务器中注册您的依赖服务,该服务器可以具有动态配置,如consulSkyDNS + 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.

相关问题 如何从 Linux 命令行或 docker 容器中检查 spring 启动 java 应用程序消耗了哪些值 - How to check what values are consumed by a spring boot java application from the Linux command line or in the docker container 使用命令行参数配置 spring 引导应用程序 - Configure spring boot application using command line argument 如何像Rake任务一样为Spring Boot应用程序执行命令行任务? - How can I execute a command line task for a Spring Boot application like a Rake task? 如何使用 docker run 命令为 Spring Boot 应用程序覆盖 application.yml 中的 spring 属性? - How can you override a spring property in application.yml for a Spring Boot application using the docker run command? 如何减少Docker中的Spring Boot应用程序内存使用率? - How can i decrease Spring Boot application memory usage in Docker? 如何使用配置文件在Docker中启动Spring Boot应用程序? - How can I start spring boot application in docker with profile? 如何在Spring Boot 2应用程序中设置Tomcat unloadDelay? - How can I set Tomcat unloadDelay in a Spring Boot 2 application? 无法通过命令行运行 Spring 引导应用程序 - Can't run Spring Boot application trough the command line 从 Linux 命令行启动 Spring 引导 web 应用程序 - Start Spring Boot web application from Linux command line 从命令行运行Spring Boot应用程序时出错 - Error running Spring Boot application from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM