It’s sometimes useful to quickly connect to JMX console, to checkout what’s going on in your application, but the whole thing get’s tricky if you’re running your app in a container. I need it from time to time and I keep myself few times searching for set of params below:

~/2021/02/how-to-run-jmx-monitoring-in-docker-image/
java \
...
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.rmi.port=${PORT1} \
-Dcom.sun.management.jmxremote.port=${PORT1} \
-Dcom.sun.management.jmxremote.local.only=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=${HOST}

The whole magic here is that PORT1 in container is app’s second port. So if you expose both HTTP and HTTPS on two separate ports, you will need here PORT2. I setup both RMI and JMX ports to same value. Second thing here is a hostname, it have to be hostname which you will use to connect to the container. If you use domain name, it have to be this domain name. If you want to use IP, set it to IP. It’s because it’s actually a virtual hostname for JMX HTTP server - if you mix them, you won’t get results. Container orchestrators often automatically export variable like HOST or HOSTNAME which you can use too hook.

After such configuration and mapping ports to container, you should be able to connect to container with VisualVMexternal link or Jconsoleexternal link .