1

I want to pass couple of variables to my springboot application as environment variables.

-Dspring.profiles.active=dev         --> spring profile
--add-opens java.base/java.lang=ALL-UNNAMED      --> needed for java 17

I managed to supply the first one using

    environment:
      - SPRING_PROFILES_ACTIVE=local

But tried different options for second. none worked.

    environment:
      - SPRING_PROFILES_ACTIVE=local
      - --add-opens java.base/java.lang=ALL-UNNAMED

    environment:
      - SPRING_PROFILES_ACTIVE=local
      - "--add-opens java.base/java.lang=ALL-UNNAMED"

    environment: JAVA_OPTS >
      SPRING_PROFILES_ACTIVE=local
      --add-opens java.base/java.lang=ALL-UNNAMED

    environment: JAVA_OPTS=-Dspring.profiles.active=dev=local --add-opens java.base/java.lang=ALL-UNNAMED

    environment: JAVA_OPTS="-Dspring.profiles.active=dev=local --add-opens java.base/java.lang=ALL-UNNAMED"

Any help is highly appreciated

Renjith
  • 1,122
  • 1
  • 19
  • 45
  • JAVA_OPTS must be passed to your ENTRY_POINT or CMD as part of the java command in order to work properly, but it causes other sort of issues. see https://stackoverflow.com/questions/53785577/passing-java-opts-to-spring-boot-application-through-docker-compose for more details. – Sombriks Mar 22 '23 at 18:26

1 Answers1

0

Ok. Finally, here is our real hero 'JDK_JAVA_OPTIONS'.

environment:
  - SPRING_PROFILES_ACTIVE=local
  - "JDK_JAVA_OPTIONS=--add-opens java.base/java.lang=ALL-UNNAMED"

My docker-compose is happy :)

| NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens java.base/java.lang=ALL-UNNAMED

JDK_JAVA_OPTIONS is only picked up by the java launcher, so use it for options that you only want to apply (or only make sense for) the java startup command. This variable is also new on JDK 9+, and will be ignored by earlier JDK versions. Hence, it's useful when migrating from older versions to 9+.

JAVA_TOOL_OPTIONS is picked up also by other java tools like jar and javac so it should be used for flags that you want to apply (and are valid) to all those java tools.

What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?

JAVA_OPTS is NOT AT ALL an option. ref: Running java with JAVA_OPTS env variable has no effect

Renjith
  • 1,122
  • 1
  • 19
  • 45