d3rbastl3r
d3rbastl3r

Reputation: 513

Set Java/JRE default settings which can be overridden by JAVA_TOOL_OPTIONS

I have multiple java spring boot services, which are running in docker containers and are configured via docker-compose. Each of those services need several java configurations which are done via JAVA_TOOL_OPTIONS in a docker-compose.yml like:

my-service:
    environment:
        JAVA_TOOL_OPTIONS: -Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdumps/my-service.hprof -XX:+ExitOnOutOfMemoryError

This works so far. My idea was to put this default configurations in to the Dockerfile like:

ENTRYPOINT ["java", "-Xmx256m", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=/tmp/heapdumps/my-service.hprof", "-XX:+ExitOnOutOfMemoryError", "-jar", "/my-service.jar"]

And this works also just fine. The problem is, that now i am not able to override those options from Dockerfile with the JAVA_TOOL_OPTIONS. So for example i am not able do deactivate the ExitOnOutOfMemoryError option by configuring this in the docker-compose.yml like:

my-service:
    environment:
        JAVA_TOOL_OPTIONS: -XX:-ExitOnOutOfMemoryError

Or even to change max heap size. Is there any possibility to achieve this behavior (so that i have my default configuration build in in to the image, but with the possibility to override single configuration options in docker-compose)?

Upvotes: 1

Views: 1921

Answers (1)

d3rbastl3r
d3rbastl3r

Reputation: 513

Finally we solved it this way:

We moved the call of the jar in to the docker-entrypoint.sh and did a call just on this .sh file. Also we added additional environment variable to store the default configuration.

Dockerfile

ENV DEFAULT_JAVA_TOOL_OPTIONS="-Xmx256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdumps/my-service.hprof -XX:+ExitOnOutOfMemoryError"

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/${project.build.finalName}.jar"]

And in the docker-entrypoint.sh we did following:

docker-entrypoint.sh

#!/bin/bash

JAVA_TOOL_OPTIONS="$DEFAULT_JAVA_TOOL_OPTIONS $JAVA_TOOL_OPTIONS"
java -jar "$1"

This way options set in JAVA_TOOL_OPTIONS will handled over the DEFAULT options

Upvotes: 2

Related Questions