Home > Net >  Spring Boot Docker Compose: How to override 'Map<String, List<String>>' envi
Spring Boot Docker Compose: How to override 'Map<String, List<String>>' envi

Time:07-26

I am using yaml properties file for the sping boot configuration.

My structure of application.yml is following:

information:
  server: ${SERVER_INFO:devserver}
  config:
    "[http://www.myshop.com]":
      - Sam
      - Joe
    "[https://www.google.com]":
      - Mary
   ... other properties of Map type.

All this values basically represent structure Map<String, List<String>> where key is site address and List is a array of the users. There could be many entries of this map, I am using this structure to read properties dynamicly.

How my docker container looks:

app:
  ports:
    - "8080:8080"
  build:
    ... i will skip this info
  image: testapp
  environment:
    - SERVER_PORT=8080
    - SERVER_INFO=QAAserver // overrided sucessfully
    - INFORMATION_CONFIG=?? // how to pass Map<Sting, List<String>> here?

So, basically, I need an ability to pass values of Map<String, List<String>> from docker compose env var to the spring boot, to override current value. How can I do it?

CodePudding user response:

Is it possible for you to write the Map<String, List> to a file, as JSON perhaps, and then pass the file directory as the value of INFORMATION_CONFIG? Then all you would have to do would be to get Docker to pass the file path to spring.

Does this help? Or is your question more about how to pass values from Docker to Spring?

Info on passing info from Docker to spring app:

https://faun.pub/how-to-pass-configurations-to-spring-app-running-on-docker-container-f6e1f0ad66c4

How do I use the JAVA_OPTS environment variable?

CodePudding user response:

JSON Objects are Map<String, Object> values.

    INFORMATION_CONFIG='{"[http://www.myshop.com]": ["Sam", "Joe"], "[https://www.google.com]": ["Mary"]}'

But you may want to use SPRING_APPLICATION_JSON for this

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.application-json

  • Related