3
votes

I need to run Java application in the Docker container with JVM options like these, but I have no idea where I can set it, I've tried use "java -Dcom...." command but it doesn't work. What is the best way to do this?

-Dcom.sun.management.jmxremote.rmi.port=9090

-Dcom.sun.management.jmxremote=true

-Dcom.sun.management.jmxremote.port=9090

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false

-Dcom.sun.management.jmxremote.local.only=false

-Djava.rmi.server.hostname=192.168.99.100

3

3 Answers

1
votes

Here my example for you:

CMD java -Xmx1024m -Xms512m -Dserver.port=8080 -jar mywar.war

Also if you have a lot of properties to add here it will be the best to create the file with environment variables. And load them to the container. And in your application use env variables to generify your app.

1
votes

If you are running with docker-compose.yml file you should add then under the specific docker entry, for example:

docker-name:

    extends:

      file: ...
      service: ...

    image: ...
    ports:
      - "9090:9090"
    environment:
      component_type: ...
      instance_id: ...
    JAVA_OPTS: "
        -Dcom.sun.management.jmxremote.rmi.port=9090

        -Dcom.sun.management.jmxremote=true

        -Dcom.sun.management.jmxremote.port=9090

        -Dcom.sun.management.jmxremote.ssl=false

        -Dcom.sun.management.jmxremote.authenticate=false

        -Dcom.sun.management.jmxremote.local.only=false

        -Djava.rmi.server.hostname=localhost"

    volumes:
       - ...
     ..

.. ..

0
votes

To pass JVM arguments to a Spring Boot application I have used the following:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.rmi.port=9010 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost"

I have a docker-compose service to specifically profile my application using something like VisualVM (which looks like you are also trying to do something similar) as I do not always want to use these configurations:

version: '3.7'
services:
  profile:
    image: my_base_image
    container_name: my_container_name
    ports:
      - '8081:8080'
      - '9010:9010'
    volumes:
      - './src:/build/src
      - './target:/build/target'
      - './logs:/build/logs'
      - './pom.xml:/build/pom.xml'
    command: 'mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.rmi.port=9010 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost"'

I simply run this to start it up: docker-compose up profile

I have written about this topic if you want to read the entire article:

https://blog.phillipninan.com/2020/08/19/diagnose-memory-leaks-in-spring-boot-with-visual-vm/