1

I'd like to use an environment variable to set -XX:HeapDumpPath option on JVM startup. Moreover, I'd like this to work inside a Docker container and in such a way that my JVM process gets PID 1. This seems to make all the proposed solutions in this question invalid for my case.

My current solution looks like this:

  • Dockerfile

    FROM openjdk:10
    
    COPY . .
    
    ENTRYPOINT [ "./start.sh" ]
    
  • start.sh

    #!/usr/bin/env bash
    
    exec java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${HEAP_DUMP_PATH} -jar build/libs/app.jar
    

If I build it with docker build --tag=test . and then run with CID=$(docker run --rm -e HEAP_DUMP_PATH="/tmp" -d test) && docker exec $CID ps -f I get:

UID PID PPID C STIME TTY TIME CMD
root 1 0 0 16:18 ? 00:00:00 java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp -jar build/libs/app.jar
root 21 0 0 16:18 ? 00:00:00 ps -f

So it seems to be working. But it also seems unnecessarily complicated. Is there a better/cleaner way to achieve this?

Update: removed -c in exec -c.

korolar
  • 1,340
  • 1
  • 11
  • 20
  • That's a very reasonable way to do it IMHO; you also might see if the `JAVA_OPTS` environment variable works for you. – David Maze Aug 31 '18 at 18:08
  • Thanks @DavidMaze, but I hope that there exists either a Docker-specific solution for expanding env. variables in `ENTRYPOINT`/`CMD` directives, or a HotSpot-specific solution for expanding env. variables in JVM configuration. Both of the above seem reasonable to me, but maybe I'm missing something. :) – korolar Aug 31 '18 at 18:26

0 Answers0