Enable JMX RMI access to a docker container

Hi Team,

At first point consider: sun.management.jmxremote dynamically assigns a second port to use for RMI (https://ptmccarthy.github.io/2014/07/24/remote-jmx-with-docker/)

These are some scenarios to we need to consider for some JMX Docker Monitoring configuration:

  • Your Local Machine: 192.168.1.10 [docker0: 172.17.0.1 --> 172.22.0.**]
  • Your Remote Machine: 192.168.1.108 [docker0: 172.17.0.1 --> 172.22.0.**]
  • JMX port trying to expose for our docker container 6001

Local (192.168.1.10) --> Local Docker Service (192.168.1.10) [Local Container Spring Boot JMX Enabled]:
This is working as expected and able to connect from jmc, jconsole, jvisualvm:
java -Dcom.sun.management.jmxremote.port=6001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar /app.jar

Or thru docker compose expose port 6001 (it does not matter if the external host port is the same or not):
ports:
- “6001”

Local (192.168.1.10) --> Non Local Docker Service (192.168.1.108) [Remote Container Spring Boot JMX Enabled]:
Suppose that you are continue using the same configuration:
java -Dcom.sun.management.jmxremote.port=6001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar /app.jar

  • 1st attempt to solve: You are ready to use Java Mission Control and start your JMC to monitor JMX Remotely and you are seeing:
    Caused by: java.rmi.ConnectException: Connection refused to host: 172.22.0.6; nested exception is: _
    _ java.net.ConnectException: Expiró el tiempo de conexión

  • 2nd attempt to solve: Then you add this parameter -Djava.rmi.server.hostname=192.168.1.108 to your JMX, start your JMC to monitor JMX Remotely and you are seeing:
    Caused by: java.rmi.ConnectException: Connection refused to host: 192.168.1.108

  • 3rd attempt to solve: Then … you have no more options … BUT this parameter is going to help us -Dcom.sun.management.jmxremote.rmi.port=6001 , start your JMC to monitor JMX Remotely and you are seeing:

java -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=6001 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.1.108 -Dcom.sun.management.jmxremote.rmi.port=6001 -jar /app.jar

With these parameters you are able to connect from jmc, jconsole, jvisualvm to remote docker containers.

In my case, I am working with docker-compose please DON FORGET to expose the SAME PORT 6001 in the remote docker host (it won’t work if you expose in another port differently that jmxremote.port and jmxremote.rmi.port):
Ports

0.0.0.0:6001->6001/tcp


Hi @gshemtov, in your case you could connect to any JMX Docker Container on the same host but consider to manage the same por for:

  • management.jmxremote.port
  • jmxremote.rmi.port
  • and port to expose