How to connect Java profiler to the module running on Docker + Vagrant

Can be used for monitoring the state of the application JVM on the fly.

Step-by-step guide

    1. Edit the Vagrantfile configuration to forward the JMX port 

              

    2. Run vagrant up

                vagrant ssh

    3. Run  docker ps  and note on which port the container in question is running on

                

    4. Stop the docker container docker stop {container_id}

    5. Run the container again with JMX port exposed and necessary variables set as JAVA_TOOL_OPTIONS

       docker run -d -t -i -e JAVA_OPTIONS='-Xmx256m' -e DB_MAXPOOLSIZE='5' -e DB_USERNAME='folio_admin' -e DB_PORT='5432' -e DB_HOST='10.0.2.15' -e DB_DATABASE='okapi_modules' -e DB_PASSWORD='folio_admin' \
      -e JAVA_TOOL_OPTIONS='-Dcom.sun.management.jmxremote.rmi.port=9010 -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.local.only=false -Djava.rmi.server.hostname=localhost' \
      -p 9010:9010 \
      -p 9169:8081 \
      localadd/mod-source-record-manager

To enable Flight Recorder pass the following options under the JAVA_OPTIONS property in the above command

 -XX:+UnlockCommercialFeatures -XX:+FlightRecorder


    
6. Run docker ps again and observe that JMX port is exposed

       

Alternatively, you can specify the same properties from JAVA_TOOL_OPTIONS in the DeploymentDescriptor.json under the 'exec' command and expose the JMX port in the Dockerfile. No need to stop and rerun the container in this case, a simple vagrant provision might do, but it is only applicable for modules deployed additionally (not those included in the vagrant box itself). See How to automate new modules’ registration within folio/testing-backend Vagrant box.


    7. Open your favorite Profiler, for example Java Mission Control, and connect to localhost:9010. Enjoy!

       

Useful links: