4

I have my spring boot application with a yml to configure it inside a 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. Does a way exist to externalize the ip, or set it from a docker command line or a better idea?

2 Answers 2

5

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)

2) Pass it as JVM argument (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)

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

See also: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Sign up to request clarification or add additional context in comments.

3 Comments

Whereas you can override some things in docker option 2 will not be preferable since it would require that you can manipulate the entrypoint when starting the docker container. Option 1 and 3 are good if only a few options (like here the url of the configserver) will have to be passed, whereas option 3 requires a particular structure of the Dockerfile. Option 4 is good if you want to overwrite a lot of settings but also requires to change defaults of Spring Boot. I personally prefer 3 with a config server and settings in git.
Thanks, I will try your advice
Does option#3 still work?? I am struggling getting it work
2

You basically have a few options from my perspective:

  1. Pass in params as environment variables from docker, e.g. by starting the app using: 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. Caveat is that you'll be bound to the same docker host.
  3. Use the same hostname and port all the time and inject the correct hostname mapping on startup of the container, e.g. configure http://configserver:8888 in your application and start the container with 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.

Whereas the last approach has the benefit that you dynamically link containers together cross nodes hosting docker containers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.